Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to store a table inside a player's folder?

Asked by 3 years ago
Edited 3 years ago

I basically have this script: is there a way i could have a table as a value? any way to do that?

local function joined(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = 'table'

    local items = Instance.new("IntValue",leaderstats)
    items.Name = "Items"
    items.Value = t{'item1','item2','item6'}

end

game.Players.PlayerAdded:Connect(joined)

thanks for your time!

2 answers

Log in to vote
1
Answered by 3 years ago

Most scripters opt against storing values in instances: Storing values in the player: good or bad

Instead, values are contained within code keyed on the player's ID/Name:

Server Script

--This is example code of a server script inside ServerScriptService

--Just initializing the userData at a high scope so all the functions can access it
local userData = {}
--Get the players service
local players = game:GetService("Players")

--This function is ran only once for each player that joins the game
local function joined(player)

    --Each player is given their own table/subTables
    --Included the Coins key just as another example of defaulting the value
    userData[player.Name] = {
        ["Items"] = {};
        ["Coins"] = 0
    }
    --Just an example of how to access the table
    userData[player.Name].Items = {"item1", "item2", "item6"}
    userData[player.Name].Coins = 500
    --[[The table now looks like this if you and I joined the game:
    userData = {
        ["Mast3rStRix"] = {
            ["Items"] = {"item1", "item2", "item6"};
            ["Coins"] = 500
        };
        ["SteelMettle1"] = {
            ["Items"] = {"item1", "item2", "item6"};
            ["Coins"] = 500
        };
    }
]]
end

--Only runs once when the player leaves the game
players.PlayerRemoving:Connect(function(player)
    --Set the player's key to nil so we don't have any nasty memory leaks
    --Obviously don't do this until any data that needs to be saved is saved
    userData[player.Name] = nil
end)

--Hook the joinedfunction to the PlayerAdded event
players.PlayerAdded:Connect(joined)

You can then easily use userData to access any user data you've defined all from one place. You can even turn it into a module script and return specified values as-needed which are up-to-date. If the client needs the information, then simply send the information over using a remote. I hope this helps, I tried to be as informative as possible

0
Oh wow This is really helpful for me!!! Im going to use this method instead. Im also going to use it for DataStores! Thank you so much! Have a great day! Mast3rStRix 43 — 3y
1
Yeah data store methods are much better! :) You can make your own (there's a great tutorial in the guides section) or you can try DataStore2 which you can download/get from the workshop :) AlexTheCreator 461 — 3y
1
No problem at all! You're absolutely right to use this for DataStores since it's a partial example of caching the data. SteelMettle1 394 — 3y
Ad
Log in to vote
1
Answered by
marfit 104
3 years ago

No. In Roblox, putting a table into something like this only gives you "Table:" and then the table's memory address. You'd be better off dividing that "Table" section in your leaderstats into the three different sections and put each value into one.

You can also run this code and turn the table into a continuous string.

local TableString = ""

for _,v in ipairs(Table) do
    TableString = TableString..v.." "
end

print(TableString)
0
Thanks ! Mast3rStRix 43 — 3y

Answer this question