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

Who can help me with a TextButton script who reset progress please?

Hey! I have a problem with a script in a button: I want to, when the button is clicked, reset the leaderboard stats (put stage to 0) and kill the player to come back to the spawn. Theres is the scripts: The button script (who dont work):

local leaderstats = Instance.new("Folder")
local button = script.Parent
local checkpoints = workspace:WaitForChild("Checkpoints")

game.Players.PlayerAdded:Connect(function(player)
	local stage = Instance.new("IntValue")
	button.MouseButton1Click:Connect(function()
		stage.Value = 0
		stage.Parent = leaderstats
		
		button.MouseButton1Click:Connect(function(Hit)
			Hit.Parent.Humanoid.Health = 0
		end)
	end)

	
end)

The leaderboard and stage script:

local checkpoints = workspace:WaitForChild("Checkpoints")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 0
	stage.Parent = leaderstats

	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)

		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)
	end)
end)

Thanks in advance for your help!

1 answer

✓ Accepted answer

The button script appears to be invalid and doesn't appear to make sense when assuming it's a separate local script running inside TextButton on the client

You need to utilize RemoteEvents (https://create.roblox.com/docs/scripting/events/remote-events-and-functions) by making TextButton send RemoteEvent signal (from the client) that causes the server to reset stage value of that specific player like this

Local Script

local remoteEvent = game.ReplicatedStorage:WaitForChild("ResetStatEvent") -- You can use any name here

script.Parent.Activated:Connect(function())
	remoteEvent:FireServer()
end)

Server Script

local checkpoints = workspace:WaitForChild("Checkpoints")
local remoteEvent = game.ReplicatedStorage:WaitForChild("ResetStatEvent") -- Again, you can use any name here

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 0
	stage.Parent = leaderstats

	player.CharacterAdded:Connect(function(char)
        	local hum = char:WaitForChild("Humanoid")
        	wait()
        	char:MoveTo(checkpoints[stage.Value].Position)

		hum.Touched:Connect(function(hit)
            		if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
            		end
		end)
	end)
end)

-- Added code here
remoteEvent.OnServerEvent:Connect(function(plr))
	if plr and plr.Character and plr.Character:FindFirstChild("Humanoid") then
		local humanoid = plr.Character.Humanoid
		plr.leaderstats.Stage.Value = 0
		humanoid.Health = 0
	end
end)

If you find the answer to be working or helped you solve the issue then please accept the answer.

2 comments

0

TY, it work very well.

0

No problem.

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