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

How do I save leaderstat values AND tools that I have?

I'm making this simulator game, I was finishing up my shop until I ran into this problem, how do I save leaderstat values and tools that I've bought? It saves the leaderstat value "Chemicals" in studios and in an actual server, but I want to save the tools that i bought as well. How do I do that?

Here's my script:

local DS = game:GetService("DataStoreService"):GetDataStore("MyLovelyGame06992299")

game.Players.PlayerAdded:connect(function(player)
 local key = "key-"..player.userId
 local leaderstats = Instance.new("Folder",player)
 leaderstats.Name = "leaderstats"
 
 local Chemicals = Instance.new("IntValue",leaderstats)
 Chemicals.Name = "Chemicals" --Value that I try to save

 local save = DS:GetAsync(key)
 if save then
  Chemicals.Value = save[1]
 else
  local load = {Chemicals.Value}
  DS:SetAsync(key,load)
 end

    while wait(1) do
	Chemicals.Value = Chemicals.Value + 15 
    end

end)

game.Players.PlayerRemoving:connect(function(player)
 local key = "key-"..player.userId
 local load = {player.leaderstats:FindFirstChild("Chemicals").Value}
 print("Doing")
 DS:SetAsync(key,load)
end)

Any help would be appreciated :)

1 comment

0

connect is deprecated, use Connect

1 answer

✓ Accepted answer

The following is an example I've used that saves tools. It's a script (SSS). The way it is setup, it assumes you have a folder in ServerStorage with the tool. The tool should also be in the workspace. You can see it checks for player backpack inventory and clones the tool, parenting it to the backpack. You can change it as needed. Good luck!

local Data = game:GetService("DataStoreService"):GetDataStore("Inventory")
game.Players.PlayerAdded:Connect(function(plr)
    local SavedItems = Data:GetAsync(plr.userId)
    if SavedItems ~= nil then
	for i,v in pairs(SavedItems)do
	    if game.ServerStorage.Inventory:FindFirstChild(v)~= nil then
		local Weapon = game.ServerStorage.Inventory:FindFirstChild(v):Clone()
		Weapon.Parent = plr.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)
    end
    if Table ~= nil then
	Data:SetAsync(plr.userId,Table)
    end
end)	

1 comment

0

Thank you! I'll try it now

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