How make my prestiges system not reset when player rejoin

Hi, I working on a game with points and prestiges. Everything working fine except the prestige reset when the player rejoin.

Explication :
When the player join to prestige you need 10 points, after 20 points, and then 30 points (+10 every prestige so right now he is at 3 prestiges) BUT when he leave and rejoin he keep his 3 prestiges but restart with 10 points, 20 points, 30 points to prestige 6 . So he paying like low prestige but he is high prestige.

If someone can tell me what to change in my current script to prevent that ?
The script :

local replicatedStorage = game:GetService("ReplicatedStorage")
local buttonEvent = replicatedStorage:WaitForChild("PrestigeEvent")
local debounce = true
local pointsRequired = 10

local function buttonPressed(plr)
	local pStats = plr:WaitForChild("leaderstats")
	local pGui = plr:WaitForChild("PlayerGui")
	local pPrestigeButton = pGui:WaitForChild("Buttons"):FindFirstChild("Prestige")
	local pPoints = pStats:FindFirstChild("Points")
	local pPrestige = pStats:FindFirstChild("Prestige")
	
	if debounce and pPoints.Value >= pointsRequired then
		debounce = false
		
		pPrestige.Value = pPrestige.Value +1
		pPoints.Value = 0
		pointsRequired = pointsRequired + 10
		pPrestigeButton.Text = "Prestige successfull"
		wait(2)
		pPrestigeButton.Text = "Prestige"
	else
		debounce = false
		
		pPrestigeButton.Text = "Points required to prestige: "..tostring(pointsRequired)
		wait(2)
		pPrestigeButton.Text = "Prestige"
	end
	wait(0.5)
	debounce = true
	
end

buttonEvent.OnServerEvent:Connect(buttonPressed)
	

Thank you everyone. :slight_smile:

You need to use DataStores to save the data. DataStoreService Easy to use DataStore system: DataStore2

My friend made me a datastore, there :slight_smile:


local DS = game:GetService("DataStoreService"):GetDataStore("WaitingSimDatastore") --DON'T CHANGE "WaitingSimDatastore" OR ELSE IT WILL RESET DATA!

game.Players.PlayerAdded:Connect(function(plr)
	--=Set up variables/values=--
	local ls = Instance.new("Folder",plr)
	local points = Instance.new("IntValue",ls)
	local mult = Instance.new("IntValue",plr)
	local prestige = Instance.new("IntValue",ls)
	--=Define variables=--
	ls.Name = "leaderstats"
	points.Name = "Points"
	points.Value = 0
	mult.Name = "Multiplier"
	prestige.Name = "Prestige"

	--=Datastore things=--
		--=/Points=--
		local id = points.Name.."-"..plr.UserId --Datastore id, should look like Points-92345351 or something idk
		local data = nil
	
		pcall (function() --Prevents script from commiting die
			data = DS:GetAsync(id) --Get data from player owo	
		end)
	
		if data ~= nil then
			points.Value = data
			print("Data loaded for "..plr.Name)
		else
			--This is a new player (or something went wrong)
			print("New Player")
			points.Value = 0
		end
		
		--=/Multiplier=--
		local multid = mult.Name.."-"..plr.UserId --Saving for the multiplier
		local multdata = nil
		
		pcall (function()
			multdata = DS:GetAsync(multid)
		end)
		
		if multdata ~= nil then
			mult.Value = multdata
		else
			mult.Value = 1
		end
		
		--=/Rebirths=--
		local prestigeid = prestige.Name.."-"..plr.UserId -- Saving for rebirths
		local prestigedata = nil
		
		pcall (function()
			prestigedata = DS:GetAsync(prestigeid)
		end)
		
		if prestigedata ~= nil then
			prestige.Value = prestigedata
		else
			prestige.Value = 1
		end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
--Save players data when they're leaving :3
	
	local id = plr.leaderstats:WaitForChild("Points").Name.."-"..plr.UserId --Datastore id (again)
	local multid = plr:WaitForChild("Multiplier").Name.."-"..plr.UserId --Datastore id (AGAIN)
	local prestigeid = plr.leaderstats:WaitForChild("Prestige").Name.."-"..plr.UserId --AAAAAAAAAAAAAAAAAAAA
	
	DS:SetAsync(prestigeid,plr.leaderstats.Prestige.value)
	DS:SetAsync(multid,plr.Multiplier.Value)
	DS:SetAsync(id,plr.leaderstats.Points.Value)
end)

--This will wait 5 seconds before letting the server close
game:BindToClose(function()
	--Make sure if server shuts down for some reason that data saves
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Server shutting down. Data was saved")
		end
	end
	
	wait(5)
	
end)

What I need to add ?

Try it before asking. It saved both of our time. Is there a problem already, if so please list it.

So, I add a variable called “Cost” into the datastore and there the script for the prestiges :

local replicatedStorage = game:GetService("ReplicatedStorage")
local buttonEvent = replicatedStorage:WaitForChild("PrestigeEvent")
local debounce = true
local players = game.Players
local pointsRequired = players:FindFirstChild("Cost")

local function buttonPressed(plr)
	local pStats = plr:WaitForChild("leaderstats")
	local pGui = plr:WaitForChild("PlayerGui")
	local pPrestigeButton = pGui:WaitForChild("Buttons"):FindFirstChild("Prestige")
	local pPoints = pStats:FindFirstChild("Points")
	local pPrestige = pStats:FindFirstChild("Prestige")
	
	if debounce and pPoints.Value >= pointsRequired then
		debounce = false
		
		pPrestige.Value = pPrestige.Value +1
		pPoints.Value = 0
		pointsRequired = pointsRequired + 10
		pPrestigeButton.Text = "Prestige successfull"
		wait(2)
		pPrestigeButton.Text = "Prestige"
	else
		debounce = false
		
		pPrestigeButton.Text = "Points required to prestige: "..tostring(pointsRequired)
		wait(2)
		pPrestigeButton.Text = "Prestige"
	end
	wait(0.5)
	debounce = true
	
end

buttonEvent.OnServerEvent:Connect(buttonPressed)
	

It don’t work and that is the error, what it mean ?

Sorry for the late reply, I was not notified of your response and I have not been on today. What was the red text above the second screenshot that you sent. You didn’t actually send the error, just the line.

No problem, the error it show me that :


Im really beginner in scripting so maybe is something really easy to solve.

local replicatedStorage = game:GetService("ReplicatedStorage")
local buttonEvent = replicatedStorage:WaitForChild("PrestigeEvent")
local debounce = true
local players = game.Players


local function buttonPressed(plr)
	local pStats = plr:WaitForChild("leaderstats")
	local pGui = plr:WaitForChild("PlayerGui")
	local pPrestigeButton = pGui:WaitForChild("Buttons"):FindFirstChild("Prestige")
	local pPoints = pStats:FindFirstChild("Points")
	local pPrestige = pStats:FindFirstChild("Prestige")
	local pointsRequired = plr:FindFirstChild("Cost")
	
	print(pointsRequired)
	print(pPoints)
	print(pStats)
	
	if debounce and pPoints.Value >= pointsRequired.Value then
		debounce = false
		
		pPrestige.Value = pPrestige.Value +1
		pPoints.Value = 0
		pointsRequired.Value = pointsRequired.Value + 20000
		pPrestigeButton.Text = "Prestige successfull"
		wait(2)
		pPrestigeButton.Text = "Prestige"
	else
		debounce = false
		
		pPrestigeButton.Text = "Points required to prestige: "..tostring(pointsRequired.Value)
		wait(2)
		pPrestigeButton.Text = "Prestige"
	end
	wait(0.5)
	debounce = true
	
end

buttonEvent.OnServerEvent:Connect(buttonPressed)