|
| 23 Apr 2016 01:46 AM |
I am terrible when it comes to datastore, this script is supposed to save all the tools the player has into a table which is saved with a datastore, then when he joins game give it back to him,
local Players = game:GetService("Players") local DataStore = game:GetService("DataStoreService") local PlayersData = DataStore:GetDataStore("ToolsSave") local ReplicatedStorage = game:GetService("ReplicatedStorage")
Players.PlayerAdded:connect(function(Player) local Key = "user_" ..Player.userId PlayersData:GetAsync(Key, function(Value) if Value then for _, DataTool in pairs (Value) do DataTool.Parent = Player.Backpack end end end) end)
Players.PlayerRemoving:connect(function(Player) local PlayersGear = {} for _, Tool in pairs (Player:WaitForCheck("Backpack"):GetChildren()) do table.insert(PlayersGear, Tool.Name) if Player and Player.Character then for _, CharacterChild in pairs (Player.Character:GetChildren()) do if CharacterChild:IsA "Tool" then table.insert(PlayersGear, CharacterChild.Name) end end end end local Key = "user_" ..Player.userId PlayersData:UpdateAsync(Key, function(OldToolTable) local NewToolTable = OldToolTable or 0 NewToolTable = PlayersGear return NewToolTable end) end)
|
|
|
| Report Abuse |
|
|
|
| 23 Apr 2016 03:51 AM |
GetAsync doesnt have 2 arguments. the Async part is put into a variable which you use to then manipulate it.
putting a second argument there just creates a function address that wont ever be called.
In the update one the function is called to overwrite the old value. |
|
|
| Report Abuse |
|
|
|
| 23 Apr 2016 04:28 AM |
This doesn't work either (I changed it to print and checked it via F9 Dev console)
local Players = game:GetService("Players") local DataStore = game:GetService("DataStoreService") local PlayersData = DataStore:GetDataStore("ToolsSave") local ReplicatedStorage = game:GetService("ReplicatedStorage")
Players.PlayerAdded:connect(function(Player) local Key = "user_" ..Player.userId local Data = PlayersData:GetAsync(Key) if Data then for _, DataTool in pairs (Data) do --DataTool.Parent = Player.Backpack print(DataTool.Name) end else local PlayersGear = {} Data = PlayersData:SetAsync(Key, PlayersGear) end end)
Players.PlayerRemoving:connect(function(Player) local PlayersGear = {} for _, Tool in pairs (Player:WaitForCheck("Backpack"):GetChildren()) do table.insert(PlayersGear, Tool.Name) if Player and Player.Character then for _, CharacterChild in pairs (Player.Character:GetChildren()) do if CharacterChild:IsA "Tool" then table.insert(PlayersGear, CharacterChild.Name) end end end end local Key = "user_" ..Player.userId PlayersData:SetAsync(Key, PlayersGear) end)
|
|
|
| Report Abuse |
|
|
|
| 23 Apr 2016 04:44 AM |
#code local players = game.Players local datastore = game:GetService("DataStoreService") local playersdata = datastore:GetDataStore("ToolsSave") local ReplicatedStorage = game:GetService("ReplicatedStorage") game.Players.PlayerAdded:connect(function(player) local key = "user_" ..player.userId if key then local async = playersdata:GetAsync(key) if async then for i, v in pairs (async) do print(v) end else local pie = {} playersdata:SetAsync(key, pie) end end end) game.Players.PlayerRemoving:connect(function(player) local key = "user_" ..player.userId local gear = {} local async = playersdata:GetAsync(key) local backpack = player.Backpack for i, v in pairs (backpack:GetChildren()) do if v and v.ClassName == "Tool" and async[v.Name] == nil then table.insert(gear, v.Name) end end playersdata:SetAsync(key, gear) end)
|
|
|
| Report Abuse |
|
|