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

How would I go about saving the stats in this script?

I want to save peoples data so that when the player joins the game again they still have their stats.


function onPlayerEntered(newPlayer)
	local NeededXP = 25
	local AddXP = 5
	wait(.5)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local score = Instance.new("IntValue")
	
	score.Name = "Money"
	score.Value = 0

	score.Parent = stats	
	stats.Parent = newPlayer
	
	local level = Instance.new("IntValue")
	
	level.Name = "Level"
	level.Value = 1

	level.Parent = stats	
	stats.Parent = newPlayer
	
	local XP = Instance.new("IntValue")
	
	XP.Name = "XP"
	XP.Value = 0

	XP.Parent = stats	
	stats.Parent = newPlayer
	while true do
		wait(0)	
		if XP.Value >= NeededXP then
			XP.Value = XP.Value - NeededXP
			NeededXP = NeededXP + AddXP
			level.Value = level.Value + 1
		end
	end
end

game.Players.ChildAdded:connect(onPlayerEntered)

2 comments

0

Use a data store. There are multiple guides here to help you get one made.

0

not sure m8

1 answer

local DS = game:GetService("DataStoreService")
local DataStore = DS:GetDataStore("Version1")

local Noob = {
	Money = 0,
	Level = 1,
	Experience = 0,
}

PlayerAdded = function(player)
	local key = "user "..player.UserId
	local Stats = Instance.new("Folder", player)
	Stats.Name = "leaderstats"
	local Level = Instance.new("IntValue", Stats)
	Level.Name = "Level"
	local Experience = Instance.new("IntValue", Stats)
	Experience.Name = "Experience"
	local Money = Instance.new("IntValue", Stats)
	Money.Name = "Money"

	
	local Data = Datastore:GetAsync(key) or Noob
	
	Level.Value = Data.Level
	Experience.Value = Data.Experience
	Money.Value = Data.SMoney

end	
-----------------------------------------------------------	

PlayerLeaving = function(player)
	local key = "user "..player.UserId
	local success, message = pcall(function()
		Datastore:SetAsync(key, {
			Level = player.leaderstats.Level.Value,
			Experience = player.leaderstats.Experience.Value,
			Money = player.leaderstats.Money.Value,
		})
	end)
	if success then
		print("Saved '"..player.Name.."' Data. " .. message)
	end
end


game.Players.ChildAdded:Connect(function(player) PlayerAdded(player) end)
game.Players.ChildRemoved:Connect(function(player) PlayerLeaving(player) end)

--------------------------------------------------------------------------------

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