#can anyone explain table to me im a beghiner
1 messages · Page 1 of 1 (latest)
ok
I'm sure he 100% understands tables and everything involving them after 30 minutes with zero prior experience in scripting/coding 😔
If it's his first language which hes learning tables can be pretty hard to understand
Yeah, which is why I was being sarcastic. Ofc he doesn't understand tables yet 😭
yeah im slow
No not really
what do you not understand
Everything icl
local _table = {"hi", "bye", 1, 3}
do you understand this
2 types: array and dictionary
a table is like a folder
it stores like vaariables ya
but i dont undertsand anything else
Im going to explain a much more complex table so u can understand like how scripters use them
ok
This is a more complex table and a complex use of a table and the syntax used in roblox lua to call items from the table
modulescript inside ServerScriptService folder
-- inside ServerScriptService you would have a modulescript containing all the games items like this
local _items = {
"sword1" = {
"name" = "basicSword",
"damage" = 0.1,
"price" = 5,
"desc" = "a basic sword"},
"sword2" = {
"name" = "goodSword",
"damage" = 0.5,
"price" = 20,
"desc" = "a good sword"},
"sword3" = {
"name" = "legendarySword",
"damage" = 1,
"price" = 100,
"desc" = "a legendary sword"}
}
return _items
You can call any data you want in a server script like this:
script inside ServerScriptService folder
local _items = require(game:GetService("ServerScriptService")._items) -- get the module script so u can use the data
print(_items.sword2.price)
-- Output would be 20
print(_items.sword3.desc)
-- Output would be "a legendary sword"
--for example if you wanted to use the sword you would simply
sword1.Activated:Connect(function()
handle.touched:Once(function(hit)
if hit:IsA("Humanoid") then
Humanoid:TakeDamage(_items.sword1.damage) -- Something like this
end
end)
end)
-- if you had an inventory system you would use a remotefunction to call a specific detail of the item you're using like this.
local returnItemEvent = game:GetService("ReplicatedStorage").returnItemEvent -- get the event
returnItemEvent.OnServerInvoke = function(player, variable, itemName)
if variable == "damage" then
for _, item in _items do -- loop through the _items table
if itemName == item.name then -- search for the item by name
return item.damage -- if the name matches, return the damage/price/desc of that item
end
end
elseif variable == "price" then
for _, item in _items do
if itemName == item.name then
return item.price
end
end
elseif variable == "desc" then
for _, item in _items do
if itemName == item.name then
return item.desc
end
end
end
end
On the localscript / guiscript you would have this
localscript inside starterGui
local returnItemEvent = game:GetService("ReplicatedStorage").returnItemEvent -- get the event
local itemName = "basicSword" -- or something
local Damage = returnItemEvent:InvokeServer("damage", itemName)
local Price = returnItemEvent:InvokeServer("price", itemName)
local Desc = returnItemEvent:InvokeServer("desc", itemName)
-- then set the textLables to its corrosponding item like this
screengui.frame.DamageText.Text = Damage
screengui.frame.PriceText.Text = Price
--etc etc you get the point...
is that too much? also if i got anything wrong i hope a better scripter than me can clear up any mistakes i made
Try to read the whole thing slowly from the top. I'm sure you'll understand it.
i understand like the thing where u can call data from it
can u call data from it inside any script in any folder
sword1.Activated:Connect(function()
handle.touched:Once(function(hit)
if hit:IsA("Humanoid") then
can u explain how that would work
so a modulescript is a script that just holds tables
ok
wdym by that
this is a example... so sword1.activated:Connect is when you use a tool in roblox.. such as a sword.. handle is the object that you put inside the tool technically its the part you hold
replicated storage is a folder that both the client and the server can see
serverscriptservice canot be seen by the client
so when hackers try to get free stuff in a game they can see anything inside the 'replicatedstorage' folder. so its not good to keep rare items inside it
do you know the difference between server and client
nope
so the server is the place that hosts the game.
the client is the player.
the player can only see specific parts of the game. such as the map, objects in the workspace and 'localscripts' which are scripts that run things like GUI which only show up on the players screen.
the server does a lot of the stuff you dont want the player to see such as store the cash a player has and rare items.
server is like roblox.com
client is your PC
ohh
thats why you use events such as returnitemevent to get details about an item such as the price because if the price of an item was on the localscript then a hacker could set the price to anything they wanted
so the server checks if the price is corret by for example
if item price == _items.item.price then
reward player item
else
print(" hacker trying to change price")
something like that
LOL
did you know there was 3 different types of scripts?
okk good i just trying to figure out how much youve learnt so far
script = server script.. anything inside this is safe from hackers
localscript = only shows on 1 players screen. so you put it inside starterGUI folder or starterplayerscripts so each player gets the script and only runs on your PC it is mostly used for menus and GUI stuff.
modulescript = holds data and reusable code good for holding item data and repeating code and can be 'required' on either local or serverscripts
Oh
I just been watching brawl dev and I'm in the leaderstats thing in his first beginer tut
The next is tablea
Tables
yes i learnt scripting from brawldev too
btw youll learn about this when you reach brawldevs video about tools and or events (i cant remember which ones first)
basically 'sword1' is a tool. The '.activated:Connect(function()' is when you click while holding a tool. and handle is the actual model or part that your character is holding.
alltogether
sword1.Activated:Connect(function() -- Click while holding sword
handle.touched:Once(function(hit) -- if the sword gets touched by something it will run this
if hit:IsA("Humanoid") then -- if the thing that touched the sword was a player then
Humanoid:TakeDamage(_items.sword1.damage) -- damage the player with 0.1 damage
end
end)
end)
--btw theres a LOT more coding to go with a sword such as animations and debounces etc but this was just an example
wait but to make a player take damage would u use .touched
because
we need to know
if it touched the player
oh ok
which in a tools case is called 'handle'
but make sure to double check online im doing thiss from memory
and my memory is shit
also theres a whole other problem with nesting :Connect functions they tend to double fire so thats why i use :Once(function()
it stops it from double firing.. so it could end up hitting the player multiple times while your swinging
or spamming the hit
yeah XD sorry exactly
i actually have no clue how much 0.1 damage is i made up a random number
wait
the max damage could be 100 which means youll have to hit them 1000 times to defeat someone with a level1 sword XD
yeah completely differentthing
ohh
you can also do
connection = nil
sword1.Activated:Connect(function() -- Click while holding sword
if connection then connection:Disconnect()
connection = handle.touched:Connect(function()
-- code here
end)
end)
its getting a bit more complex this stuff youll notice it when you start coding and suddenly buying something twice starts costing double each time you purchase it then youll use this
ohh
nah you're all goods dude - i didnt really start coding til like 6-8 months ago
Mmmm, yes, hit:IsA("Humanoid")
you could atleast tell us how to correctly detect a player
i havent needed to detect a player in my game so i kinda guessed
oh lol
i know its close to that tho XDD
Well, there was a few problems with the snippet I responded to, I can list them rq. General idea is sound though
yes i appreciate all criticism
First problem is you shouldn't do .Touched:Once() for this. This is because if it touches anything, regardless if it's an enemy, it'll disconnect the function.
Another problem is that the "hit" refers to the part getting hit. So, to detect if it's a character getting hit, you'll want to get hit.Parent and then find a humanoid in it using :FindFirstChild() or :FindfirstChildOfClass
yooo yeah thats true
FindFirstChild() basically allows you to detect anything without it instantly erroring out
Also, btw, damage refers to how much health should be taken away from the humanoids health property. A base player with no alterations has 100 base health
cuz if it wasnt a humanoid it would error
i did see somewhere i think one of the playerscripts it said health = 100
so i did assume 100 somewhere
its just a really shit sword
only 1000 hits
atleast the legendary sword only takes 100 hits to kill the player 😂
local connection = nil
sword1.Activated:Connect(function()
--add animation and sound effects
connection = handle.touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(_items.sword1.damage)
connection:Disconnect()
end
end)
end)
still going off memory i think this is probably atleast closer to being correct.
yoo im learning tables
i learnt that pairs() iterates through dictionaries
and ipairs iterates through the other
arrays need to be like indexed to get the elemnent
and dictionaries just need something like the table name i think and the element inside
no index
Thing is, roblox is actually optimized for generalized iteration, so in most cases you don't have to use either ipairs or pairs. Generalized iteration is also more readable (and performant)
how do u do that
By simply not putting either pairs or ipairs.
for i, v in pairs(table) do
Would be
for i, v in table do