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

Is it a good idea to make a Name the key of DataStore? Is there any other safer way than this?

Asked by 5 years ago
       local DataStore = game:GetService("DataStoreService"):GetDataStore('ToolSave')
game.Players.PlayerAdded:Connect(function(plr)
    local savedstuff = DataStore:GetAsync(plr.userId)
    if savedstuff ~= nil then
        for i,v in pairs(savedstuff) do
            if game.ServerStorage.Inventory:FindFirstChild(v) ~= nil then --Inventory is a folder with Weopons inside it 
                local weopon = game.ServerStorage.Inventory:FindFirstChild(v):Clone()
                weopon.Parent = plr:WaitForChild('Backpack')

            end
        end
    end
plr.CharacterRemoving:Connect(function()
    plr.Character.Humanoid:UnequipTools()

end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local Table = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
    table.insert(Table,v.Name) --It uses name from what I understand

end
if Table ~= nil then
    DataStore:SetAsync(plr.userId,Table)
end
end)

1 answer

Log in to vote
1
Answered by
farizarps 132
5 years ago

Players can change their name so using a name as a key is a bad idea. if a player changes their name when they rejoin your game it would make a new datastore for them as if they are a new player.

A better alternative is to use a players userId. every player is assigned a unique userId that never changes so you would be better off using it.

to use it is simply player.userId and in your case,

for i,v in pairs(plr.Backpack:GetChildren()) do
    table.insert(Table,v.userId) --It uses name from what I understand 
end
0
Thats not what I mean, I need a way to classify the Tools in the Tables and Inventory because Just one change = wipeout of data User#22788 5 — 5y
0
you could have a table of tools and each tool has a unique number farizarps 132 — 5y
Ad

Answer this question