#can anyone explain table to me im a beghiner

1 messages · Page 1 of 1 (latest)

open veldt
#

im a beginier to scripting

quaint lily
#

Tables hold things

#

👍

open veldt
#

ok

quaint lily
#

Also just look them up on youtube

mighty herald
#

@open veldt do you understand them yet

#

@open veldt ?

quaint lily
mighty herald
#

If it's his first language which hes learning tables can be pretty hard to understand

quaint lily
mighty herald
#

yeah im slowbigbrain

open veldt
mighty herald
#

what do you not understand

open veldt
#

Everything icl

mighty herald
#
local _table = {"hi", "bye", 1, 3}

do you understand this

wanton eagle
#

2 types: array and dictionary

sleek sky
#

a table is like a folder

open veldt
#

but i dont undertsand anything else

sleek sky
open veldt
#

ok

sleek sky
#

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

sleek sky
#

Try to read the whole thing slowly from the top. I'm sure you'll understand it.

open veldt
#

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

sleek sky
#

so a modulescript is a script that just holds tables

open veldt
#

ok

sleek sky
#

you can 'require' module scripts anywhere

#

aslong as its in replicatedstorage

open veldt
#

wdym by that

sleek sky
sleek sky
#

serverscriptservice canot be seen by the client

open veldt
#

oh

#

does the client tell the server what to do

sleek sky
#

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

sleek sky
sleek sky
#

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.

open veldt
#

ohh

sleek sky
#

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?

open veldt
#

Yea

#

module local and normal

#

i dint know what they did

sleek sky
#

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

open veldt
#

Oh

#

I just been watching brawl dev and I'm in the leaderstats thing in his first beginer tut

#

The next is tablea

#

Tables

sleek sky
#

yes i learnt scripting from brawldev too

sleek sky
#

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
open veldt
#

wait but to make a player take damage would u use .touched

#

because

#

we need to know

#

if it touched the player

sleek sky
#

yeah

#

prettuy much

#

but for the part that is the sword

open veldt
#

oh ok

sleek sky
#

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()

open veldt
#

oh

#

so it runs the function 1nce

sleek sky
#

it stops it from double firing.. so it could end up hitting the player multiple times while your swinging

#

or spamming the hit

sleek sky
#

i actually have no clue how much 0.1 damage is i made up a random number

open veldt
sleek sky
#

the max damage could be 100 which means youll have to hit them 1000 times to defeat someone with a level1 sword XD

open veldt
#

doesnt connect paralell do the same thing

#

runs it once

sleek sky
#

connect parallel is running on a different cpu core

#

so you can prevent lag

open veldt
#

what

#

oh

sleek sky
#

yeah completely differentthing

open veldt
#

ohh

sleek sky
#

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

open veldt
#

ohh

sleek sky
#

keep watching brawldev its worth it

#

things will make sense as you go

open veldt
#

ok

#

i havent watched or scripted in quite some time

#

like a month

sleek sky
#

nah you're all goods dude - i didnt really start coding til like 6-8 months ago

quaint lily
sleek sky
#

i havent needed to detect a player in my game so i kinda guessed

quaint lily
#

oh lol

sleek sky
#

i know its close to that tho XDD

quaint lily
#

Well, there was a few problems with the snippet I responded to, I can list them rq. General idea is sound though

sleek sky
#

yes i appreciate all criticism

quaint lily
#

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

sleek sky
#

yooo yeah thats true

#

FindFirstChild() basically allows you to detect anything without it instantly erroring out

quaint lily
sleek sky
#

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 😂

sleek sky
#
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.

open veldt
#

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

quaint lily
open veldt
#

how do u do that

quaint lily