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

Script To Save data leaderstats and where to make script?

i need to know how to save for my simulator so yeah im kinda dumb as crap :D so yeah also if u can help me with developer products that increase leader stats that would help 2 :D my leaderstat is called "Muscle"

3 answers

Here, I wrote a script for you:

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local MuscleValue = player:WaitForChild("leaderstats"):WaitForChild("Muscle")

	local Data

	local Success, ErrorMessage = pcall(function()
		Data = DataStore:GetAsync(player.UserId.."-Muscle")
	end)

	if Success then
		MuscleValue.Value = tonumber(Data)
	else
		print("There was an error loading "..player.Name.."'s data.")
		warn(ErrorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Success, ErrorMessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-Muscle", player:FindFirstChild("leaderstats"):FindFirstChild("Muscle"))
	end)

	if Success then
		print(player.Name.."'s Data was successfully saved.")
	else
		print(player.Name.."'s Data failed to save.")
		warn(ErrorMessage)
	end
end)

An in-depth explanation would be very long and impractical to put here, so here's a link to one if you would like to know how this script works.

https://www.youtube.com/watch?v=DkYupSBUpes

3 comments

0

To use this script, place I would recommend placing it inside of ServerScriptService.

0

didnt work'

0

my tool isnt owrkin now....

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