Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to datastore a ingame value? and load it when player joins again ?

i want a value to be saved but it is in game.workspace.Backpack.Capacity.Value

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("StorageSave")

game.Players.PlayerAdded:Connect(function(player)
   pcall(function()
      local Storage = dataStore:GetAsync("User_"..player.UserId)
        if Storage then
          local value = game.Workspace.Backpack.Capacity.Value
            if value then
               game.Workspace.Backpack.Capacity.Value = value    

        end
     end
   end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()

      local Backpacksave = {}
       
       table.insert(Backpacksave)

       dataStore:SetAsync("User_"..player.UserId,Backpacksave)
   end)
end)

this does not save the value please help me

2 answers

✓ Accepted answer
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("StorageSave")

game.Players.PlayerAdded:Connect(function(player)
   pcall(function()
      local Storage = dataStore:GetAsync("User_"..player.UserId)
        if Storage then
          game.Workspace.Backpack.Capacity.Value = Storage -- loads your data as the value
	else
	  game.Workspace.Backpack.Capacity.Value = 0 -- I would give the player a default value if they're new to the game
        end
     end
   end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
	-- Not sure what you're trying to do with the table. I thought maybe you just wanted to save the value here
	dataStore:SetAsync("User_"..player.UserId,game.Workspace.Backpack.Capacity.Value)
   end)
end)

game:BindToClose(function() -- I would add a :BindToClose because if you're the only player in the server and you leave, your remaining functions such as saving your data, may not finish running because the server can shut down before that is done
    for _, player in pairs(game.Players:GetPlayers()) do -- loops through all the players so the bound function here can run for them
	dataStore:SetAsync("User_"..player.UserId,game.Workspace.Backpack.Capacity.Value) -- saved their data before the server shuts down
    end
end)

2 comments

0

thank you so much i dont know why i am so stupid to add the table but thank you so much

0

No problem! Glad I could help!

The problem with Players.PlayerRemoving is that it doesn't count for the last player in the server. When the last player leaves, the game shuts down straight away. A way to solve that is to use game:BindToClose(). game:BindToClose() runs the code for 30 seconds before the server shuts down. Also, you should use pcall properly. pcall catches errors, and allows you to save your data. Here's how it works:

local success, errorMessage = pcall(function()
	dataStore:SetAsync("User_"..player.UserId,Backpacksave) --Change this to GetAsync for player added
end)
if success then
	print("Data Saved!")
	--game.Workspace.Backpack.Capacity.Value = value --Use this if you're GetAsync
else
	warn("There was an error while saving data: "..errorMessage)
end

Sources: game:BindToClose()

1 comment

0

thanks this also helped me with other scripts

This discussion is preserved as it was. New answers, comments, and votes are closed.