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

How do I save these leaderstats?

Hello! I'm currently making a game that requires leaderstats, (Trolls, Gems, Rebirths) and I am attempting to find a way to save these. I have tried watching some tutorials/youtube videos, yet none of them work.

Here's my leaderstats script:

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

	if not Player:FindFirstChild("leaderstats") then
		local Folder = Instance.new("Folder" ,Player)
		Folder.Name = "leaderstats"
	end

	local Folder = Player:WaitForChild("leaderstats" ,5)

	local Value1 = Instance.new("IntValue",Folder)
	local Value2 = Instance.new("IntValue",Folder)
	local Value3 = Instance.new("IntValue",Folder)


	Value3.Name = "Trolls"
	Value1.Name = "Gems"
	Value2.Name = "Rebirths"
	
end)

Any help or advice would be heavily appreciated!

Thanks

3 comments

0

I updated my answer it should work now

0

turn on API services in game setting under security

2 answers

Here is a simply datastore system I did the first 2 for you. For practise try doing the 3rd.

Old Answer Sorry I made some mistypes this should work now I tested it btw Script:

local DSS = game:GetService("DataStoreService")

local Troll = DSS:GetDataStore("TrollData")

local Gems= DSS:GetDataStore("GemsData")--Duplicate this and change name to rebirths



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

	local leaderstats = Instance.new("Folder", player)

	leaderstats.Name = "leaderstats"



	local Troll = Instance.new("IntValue", leaderstats)

	Troll.Name = "Troll"



	local gems = Instance.new("IntValue", leaderstats)--Duplicate this and change name to rebirths

	gems.Name = "Gems"--Duplicate this and change name to rebirths



	local playerId = "Player_"..player.UserId



	local Trolldata

	local Gemsdata--Duplicate this and change name to rebirths

	local success, errormessage = pcall(function()

		Trolldata = Troll:GetAsync(playerId)

		Gemsdata = Gems:GetAsync(playerId)--Duplicate this and change name to rebirths

	end)



	if success then

		Troll.Value = Trolldata

		Gems.Value = Gemsdata--Duplicate this and change name to rebirths

	end

end)



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

	local playerId = "Player_"..player.UserId



	local Trolldata = player.leaderstats.Troll.Value

	local Gemsdata = player.leaderstats.Gems.Value--Duplicate this and change name to rebirths



	local success, errormessage = pcall(function()

		Troll:SetAsync(playerId, Trolldata)

		Gems:SetAsync(playerId, Gemsdata)--Duplicate this and change name to rebirths

	end)



	if success then

		print("Saved")

	else

		print("There was an error")

		warn(errormessage)

	end

end)

Hope this helps!!

Updated Script

local DSS = game:GetService("DataStoreService")

local Troll = DSS:GetDataStore("TrollData")

local Gems= DSS:GetDataStore("GemsData")--Duplicate this and change name to rebirths

local Rebirth = DSS:GetDataStore("RebirthData")


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

	local leaderstats = Instance.new("Folder", player)

	leaderstats.Name = "leaderstats"



	local Troll = Instance.new("IntValue", leaderstats)

	Troll.Name = "Troll"



	local gems = Instance.new("IntValue", leaderstats)--Duplicate this and change name to rebirths

	gems.Name = "Gems"--Duplicate this and change name to rebirths

local Rebirth = Instance.new("IntValue", leaderstats)

	Rebirth.Name = "Rebirth"

	local playerId = "Player_"..player.UserId



	local Trolldata

	local Gemsdata--Duplicate this and change name to rebirths
local Rebirthdata

	local success, errormessage = pcall(function()

		Trolldata = Troll:GetAsync(playerId)

		Gemsdata = Gems:GetAsync(playerId)--Duplicate this and change name to rebirths
Rebirthdata = Rebirth:GetAsync(playerId)
	end)



	if success then

		Troll.Value = Trolldata

		Gems.Value = Gemsdata--Duplicate this and change name to rebirths
		Rebirth.Value = Rebirthdata
	end

end)



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

	local playerId = "Player_"..player.UserId



	local Trolldata = player.leaderstats.Troll.Value

	local Gemsdata = player.leaderstats.Gems.Value--Duplicate this and change name to rebirths
	local Rebirthdata = player.leaderstats.Rebirth.Value


	local success, errormessage = pcall(function()

		Troll:SetAsync(playerId, Trolldata)

		Gems:SetAsync(playerId, Gemsdata)--Duplicate this and change name to rebirths
Rebirth:SetAsync(playerId, Rebirthdata)
	end)



	if success then

		print("Saved")

	else

		print("There was an error")

		warn(errormessage)

	end

end)

15 comments

0

Also I just notice Troll in my script isn't plural hope that is ok

0

Still doesnt work after using the updated script. It says in output 'Saved' when I leave, but it doesn't load it back in once I join back

0

turn on API services in game setting under security

0

I have

0

they on?? Google it if you need help lots of tutorials on how to turn them on

0

Also try testing the script before you add 3rd leaderstat maybe u added it wrong

0

i added it all correct i did what it told me to do

0

weird it works for me though

0

maybe try send me the script with rebirths already because I might be making a mistake and I dont realise

0

OK

0

I updated my answer copy the Updated script

0

Before GetAsync, Troll is defined as an Instance, not a DataStore.

0

so how would i fix that?

0

Opps line 46 where it says Rebirthdata = Troll change Troll to Rebirth

0

That should fix it I will update my script

Doesn't seem to work. It has red lines under every time it says TrollData, GemsData and RebirthsData (the new one I made for rebirths). I don't know why. An answer would be appreciated

3 comments

0

Show us the error output

0

There was no error in the output, it just didn't work

0

I updated my answer try it

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