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

Trouble saving leaderstats. help?

I am having no errors yet it wont save.

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

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

local stats = Instance.new("IntValue")

stats.Name = "leaderstats"

stats.Parent = plr

local Soul = Instance.new("IntValue")

Soul.Name = "Screams"

Soul.Parent = stats

local keyboi = "player-"..plr.userId

local savedvalues = DS:GetAsync(keyboi)

if savedvalues then

Soul.Value = savedvalues

else

DS:SetAsync(keyboi,Soul.Value)

end

end)

  

game.Players.PlayerRemoving:Connect(function(plr)

local keyboi = "player-"..plr.userId

DS:SetAsync(keyboi,{plr.leaderstats.Souls.Value})

end)

2 comments

0

Are your variables correct? Do they match up according to what you have in your game?

0

you never created a stat named "Souls" you named one "Screams" tho

2 answers

✓ Accepted answer
local DS = game:GetService("DataStoreService"):GetDataStore("StorageThing")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"
	stats.Parent = plr
	local Soul = Instance.new("IntValue")
	Soul.Name = "Screams"
	Soul.Parent = stats

	local keyboi = "player-"..plr.userId
	local savedvalues = DS:SetAsync(keyboi)

	if savedvalues then
		Soul.Value = savedvalues[1] -- you're trying to set the value to a table, should be savedValues[1] instead
	else
		DS:SetAsync(keyboi,{Soul.Value})
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local keyboi = "player-"..plr.userId
	DS:SetAsync(keyboi, {plr.leaderstats.Screams.Value}) 
	-- you named the stat "Screams" so it would be 'plr.leaderstats.Screams'
end)

1 comment

0

Hell there I am almost ready to publish my game but the thing is that my cash in leaderstats are not working,take for example it works it saves and then I do a small change in the roblox studio not even about scripts and the leaderstats are gone and back to 0. Every time u step on money it gives you 10 cash and so as I’m working in studio it most often doesn’t save and goes back to 0. pls help me

Where you declared the Soul variable on the bottom of your code you put leaderstats.Souls Its just an error.

2 comments

0

Edit: Nvm went by the script variables. Not by the name given. Ignore the answer.

0

You can remove or update it if you wish

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