Hi! So i was making my new game called turret tycoon and I made a script where when you leave it saves your bought items in the tycoon, and when you rejoin it loads the tycoon.
for some reason it doesnt load back up again
heres my script
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("playerstore")
local Items = workspace.Tycoon
local function create_table()
local ReturnTable = {} -- Returns the table
local Item_Table = {}
table.insert(ReturnTable, Item_Table)
return ReturnTable
end
game.Players.PlayerAdded:Connect(function(plr)
local Key = "id_"..plr.UserId
local Data
local success, err = pcall(function() -- I put this into a pcall function to handle errors
Data = ds:GetAsync(Key)
end)
if success and Data ~= nil then
local ItemTable = Data[1] -- Getting sub tables
for _, v in ipairs(ItemTable) do -- Looping through the loaded table checking for the item
if Items:FindFirstChild(v) then
local LoadedModel = Items:FindFirstChild(v)
LoadedModel:Clone().Parent = workspace.Tycoons -- Cloning the loaded model
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Key = "id_"..plr.UserId -- Data key
local Table = create_table() -- Call the function with parameter
local success, err = pcall(function() -- This function handles errors
ds:SetAsync(Key, Table) -- Saving to the data store
end)
if success then
print("Saved Tycoon!")
end
if not success then -- if saving failed it will warn the error in the output.
warn(err)
end
end)
I got pretty confused on how this doesnt work. If you have better improvements to the script let me know please!
Icy,