What I am attempting to achieve
In the past couple days, I have been attempting to make a Laps system where a player, if they cross the start/finish line and if they touched a checkpoint (which triggers a boolean value to true if they touched the checkpoint), they would add one lap to the player (which sets that same boolean value to false).
Issue
I don't know how to add the laps to the player once the player touches the start/finish line.
My script is inside the FinishLine part.
local FinishLine = script.Parent
local hasHitCheckPoint = workspace.Checkpoint.hasHitCheckpoint
FinishLine.Touched:Connect(function(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if (humanoid and hasHitCheckPoint.Value == true) then
hasHitCheckPoint.Value = false
if hasHitCheckPoint.Value == false then
-- Do this
end
end
end)
Keep in mind that Laps is an IntValue and is within the leaderstats folder.
To add a value to the leaderstats, you would find the leaderstats value you are looking for, in this case, Laps.
EDIT: This goes inside the script where you put the comment “–Do this”
First you need to find the player.
local player = game.Players:GetPlayerFromCharacter(partParent)
Then find the leaderstat and add one to it.
local player = game.Players:GetPlayerFromCharacter(partParent)
local stat = player:WaitForChild('leaderstats'):WaitForChild('Laps')
stat.Value = stat.Value+1
EDIT: Also don’t forget the debounce. Since this is a server script I would advise keeping a table with players who are in the debounce list and removing them after around 1 second.
So it could look something like:
local debounceGroup = {}
local function updateDebounceGroup(player)
if debounceGroup[player] then
debounceGroup[player] = nil
else
debounceGroup[#debounceGroup+1] = player
end
end
1 Like
Firstly u need a debounce in that or u r gonna have players getting hundreds of points very quickly, secondly you would do:
game.players[hit.Parent.Name].leaderstats.Laps.Value = game.players[hit.Parent.Name].leaderstats.Laps.Value + 1
In line 7 of my code, it detects if a Humanoid touched it and if a BoolValue is true. No need for a debounce.
if (humanoid and hasHitCheckPoint.Value == true) then -- If a humanoid touches the part AND if a BoolValue is true, do this.