Leaderstats increase script not working

local players = game:GetService("Players")

local function onPlayerAdded(player)

	local wipeouts = player.leaderstats.XP
	
	task.spawn(function()
		while player and player.Parent do
			task.wait(300)
			wipeouts.Value += 5
		end
	end)
end

players.PlayerAdded:Connect(onPlayerAdded)

this is a script in serverscriptservice for a value on the leaderstats to increase after 300 seconds, why doesnt this work? the leaderstats is handled in another script

1 Like

Is there any errors? Or anything that could help? Plus you do not need that task.spawn here you can do this:

local players = game:GetService("Players")

local function onPlayerAdded(player)

	local wipeouts = player.leaderstats.XP

	while player and player.Parent do
		task.wait(300)
		wipeouts.Value += 5
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
2 Likes

I would recommend changing your code to use WaitForChild (assuming your leaderstats are created by another script) to avoid race conditions, which could be causing an issue with your current script. for testing purposes you could also change the 300 second wait time to like 5 seconds

local players = game:GetService("Players")

local function onPlayerAdded(player)

	local wipeouts = player:WaitForChild("leaderstats"):WaitForChild("XP")
	
	while player and player.Parent do
		task.wait(300)
		wipeouts.Value += 5
	end
end

players.PlayerAdded:Connect(onPlayerAdded)

as @sfgij asked, are there any errors?
also, you do not need task.spawn for this unless you plan on running code after your while loop in the same function

2 Likes

Thanks! Tried this and it worked, thank you to @sfgij as well for helping out

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.