Difficulties making a button which resets a players checkpoint

Hey guys, I created a checkpoint system which uses API to save your data.

I wanted to make a button which uses click detectors and resets the player back to checkpoint 1 but I’m having trouble can anyone help? Here is the script I tried and did not work.
I’ve tried seeing posts on how to fix this. I still got no clue, the regular checkpoints are basically the same script it is just the click function is changed to a touch function and the values add up.

Just a side note I am using a savescript which uses the leaderboard.

local Players = game:GetService("Players")

for i, Stage in ipairs(Stages:GetChildren()) do
	script.Parent.ClickDetector.MouseClick:Connect(Players)
		if Players.ClassName == "MeshPart" then
			local character = Players:FindFirstAncestorWhichIsA("Model")

			if character and Players:GetPlayerFromCharacter(character) then
				local humanoid = character:FindFirstChildWhichIsA("Humanoid")

				if humanoid then
					local player = Players:GetPlayerFromCharacter(character)
					local PlayerStage = player:WaitForChild("leaderstats").Stage
					local StageNumber = tonumber(Stage.Name)

					if StageNumber == PlayerStage.Value - 1 then
						PlayerStage.Value = StageNumber

					elseif StageNumber > PlayerStage.Value - 1 then
						humanoid.Health = 0
					end
				end
			end
		end
	end)
end```
3 Likes

In line 4, didn’t you mean

script.Parent.ClickDetector.MouseClick:Connect(function(Players)

?

3 Likes

I just tried it nothing changed, and nothing shows in the output either

In the output it says, studio access to API services is not allowed.
Just go into game settings > security > access to API services

2 Likes

Oh thats for the save script it’s not relevant to the reset script, I just tried it and it did not work, nothing shown in the output either.

But you should still enable API services, otherwise your whole system would not work

Try this

local Players = game:GetService("Players")

for i, Stage in ipairs(Stages:GetChildren()) do
	script.Parent.ClickDetector.MouseClick:Connect(Players)
		if Players.ClassName == "Player" then -- unnecessary but okay
			local character = Players. Character

			if character then
				local humanoid = character:FindFirstChildWhichIsA("Humanoid")

				if humanoid then
					local player = Players
					local PlayerStage = player:WaitForChild("leaderstats").Stage
					local StageNumber = tonumber(Stage.Name)

					if StageNumber == PlayerStage.Value - 1 then
						PlayerStage.Value = StageNumber

					elseif StageNumber > PlayerStage.Value - 1 then
						humanoid.Health = 0
					end
				end
			end
		end
	end)
end```

From my understanding, you’re either using a local script inside of SSService, or you’re using a server script, which results in you not being able to reference the local player there.

Just replying to both of your comments and nothings changed, with API enabled heres the results (it still isn’t fixed)

Can you add print statements to some lines in the script? And see which one doesn’t print

No difference, now something is being put through the output,
image

I made a typo do Connect(function(Players) instead of just Connect(Players)
There will probably be an end error

Yeah there is

Add an end to the bottom of the code

Yes it works! I just tried it thanks alottt!!!

Thanks to grassperson (neweve2323), The finished script that works is,

local Players = game:GetService("Players")

for i, Stage in ipairs(Stages:GetChildren()) do
	script.Parent.ClickDetector.MouseClick:Connect(function(Players)
	if Players.ClassName == "Player" then
		local character = Players. Character

		if character then
			local humanoid = character:FindFirstChildWhichIsA("Humanoid")

			if humanoid then
				local player = Players
				local PlayerStage = player:WaitForChild("leaderstats").Stage
				local StageNumber = tonumber(Stage.Name)

				if StageNumber == PlayerStage.Value - 1 then
					PlayerStage.Value = StageNumber

				elseif StageNumber > PlayerStage.Value - 1 then
					humanoid.Health = 0
					end
				end
			end
		end
	end)
end
1 Like

You should mark his answer as the solution, not your own reply.

his script was broken I just fixed it with the end’s dont worry i credited him

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