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

How to fix bug with leaderstats data saving?

I need to save leaderstats in DataStore. My leaderstats are:

Cash

InBucket

Catches

I need a universal but simple script for saving these, and also other values I didn't mention. I know there ARE TONS of tutorials and guides for it but seems like none helped me yet (out of date or too complex). Also, I already have these values created in a script, and I want to save in another, so please do not make them again. (IK it isn't a request site, if you are against requesters, try to at least explain.) Please help... Any help is much appreciated. Also, I deleted the script cause it didn't work, if you could explain me how to create new simple one, i would really appreciate it :)

1 answer

✓ Accepted answer

Ok I wrote this script for you that should save the stats. If you have any errors dm me and I will try to fix them. You could also look at the wiki for later stuff, for example DataStore in this case https://developer.roblox.com/api-reference/function/DataStoreService/GetDataStore .

local DataStore = game:GetService("DataStoreService")
local Http = game:GetService("HttpService")
local Stats = DataStore:GetDataStore("Stats")

function GetStats(Player)
	local Success,Return = pcall(function()
		return Stats:GetAsync(Player.UserId)
	end)
	if Success then
		Return = Http:JSONDecode(Return)
		return Return
	else
		print("Getting data error :"..Return)
		return {}
	end
end

function SetStats(Player)
	local info = {}
	info.Cash = Player.Leaderstats.Cash.Value
	info.InBucket = Player.Leaderstats.InBucket.Value
	info.Catches = Player.Leaderstats.Catches.Value
	info = Http:JSONEncode(info)
	local Success,Error = pcall(function()
		Stats:SetAsync(Player.UserId,info)
	end)
	if not Success then
		print("Setting data error :"..Error)
	end
end

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Stats = GetStats(Player)
	Player:WaitForChild("Leaderstats"):WaitForChild("Cash").Value = Stats.Cash
	Player:WaitForChild("Leaderstats"):WaitForChild("InBucket").Value = Stats.InBucket
	Player:WaitForChild("Leaderstats"):WaitForChild("Catches").Value = Stats.Catches
end)

Players.PlayerRemoving:Connect(SetStats)

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