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```
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.
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