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

Data Stores(Saving Tools) With Loops?

Asked by 8 years ago
Edited 8 years ago

SO here is my code(With error):


------------------------------------ game.Players.PlayerAdded:connect(function(player) local DataStore = game:GetService("DataStoreService"):GetDataStore(player.userId) if player.Name ~= "Player" then player:WaitForDataReady() --Safely Load Data-- local AllTools = game.lighting:GetChildren() local S1 = DataStore:GetAsync("Tools") for i,v in pairs(AllTools) do if v.Name == S1[i.Name] then -- attempt to index I (a Num Value) v:Clone().Parent = player.Backpack end end end end) game.Players.PlayerRemoving:connect(function(player) game.OnClose = function() pcall(function() local DataStore = game:GetService("DataStoreService"):GetDataStore(player.userId) local Tools = player.Backpack:GetChildren() local s1 = {} for i=1,#Tools do if Tools[i].ClassName == "Tool" then local n = i s1[n] = Tools[i].Name end end DataStore:SetAsync("Tools",s1) end) end end) ---------------------------------------

SO the idea is that your tools save with data stores with the names being set as a string, then when they load back into the game it clones the tools from lighting, going through the table, then the tools in lighting. This is a simple sketch I whipped up but how would I get it to load the table back in? (around line 12 where the error is) And would this code necessarily save all tools? because if you have a tool equipped it goes to the player and not the backpack, how would I add that too? All help appreciated, Thx, Bubs

2 answers

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

There are a lot of problems with the way you are using the data store in your code, 1st you should not be creating a new data store for every player as you should be changing the key, 2nd you should not be using lighting to sore object and lastly you should not be using the on close event like this. You need to save the data when the player leaves the game not when the server closes.

Example:-

-- we get the data store at the top so we don't need to get it each time a plr is added / removed
-- The current setup tries to get a new data store for every player, you should be using one data store and changing the keys
local dsServ = game:GetService('DataStoreService'):GetDataStore('Testing')

-- Tools should be stored in ServerStorage and not in lighting
local toolList = {}

for _, tool in pairs(game:GetService('ServerStorage'):GetChildren()) do
    -- just make this into a key value pair so we dont need to loop to find each tool
    toolList[tool.Name] = tool -- tools must have different names else it will be overwritten
end

game.Players.PlayerAdded:connect(function(plr)

    -- we use a a key to access the player data, this key is made unique with the UserId
    local key =  'tools_' .. tostring(plr.UserId)

    local data  = dsServ:GetAsync(key)

    local backPack = plr:WaitForChild('BackPack')
    if data then
        -- data found
        for _, tool in pairs(data) do
            if toolList[tool.Name] then toolList[tool.Name]:Clone().Parent = backPack end -- add tool if found in the list
        end
    else
        -- no data found 
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = 'tools_' .. tostring(plr.UserId)

    -- build a list of tools
    local saveList = {}
    for _, tool in pairs(plr.BakcPack:GetChildren()) do
        table.insert(saveList, tool.Name)
    end

    -- save tool list
    dsServ:SetAsync(key,saveList)
end)

I hoe this example helps, please comment if you do no understand how / why this code works.

0
thank you very much man! However my game is old and unfortunatly if all my tools were in a different parent the game would break, so instead of me spending hours fixing my game, is there a way where i can just check the class in a for loop for tools in lighting? Bubbles5610 217 — 8y
0
Yes there is, just use an if statment. It would be best to update the game though as this change would make it more efficient. User#5423 17 — 8y
0
right okay,thanks man! Bubbles5610 217 — 8y
0
hmm it seems to be saving okay but not loading in :/ its throwing no errors Bubbles5610 217 — 8y
View all comments (2 more)
0
1st time it would not have any data to load. User#5423 17 — 8y
0
i ran through with print errors and it seems to be line 24, the if statement doesnt seem to work :/ maybe its because its to do with lighting as the parent? Bubbles5610 217 — 8y
Ad
Log in to vote
0
Answered by
iFlusters 355 Moderation Voter
8 years ago
Edited 8 years ago

Quote from the wiki:

Keys must be strings. The value is the information you want to store or access. Values can be numbers, booleans, strings, or Lua tables. However, values cannot be ROBLOX types like Vector3, Instance, or Part.

http://wiki.roblox.com/index.php?title=Data_store

Instead you could save the tools names then find them in say ReplicatedStorage then put them in the player's backpack.

0
i think theyre set in strings, i changed the code a bit, but that doesnt quiet help anwser my code error Bubbles5610 217 — 8y
0
You cannot store the Class Tool. iFlusters 355 — 8y
0
line 28 - im storing the name of the tool though? Bubbles5610 217 — 8y
0
No you're storing its name, which is a string. iFlusters 355 — 8y

Answer this question