So a bit of back story before I explain the question more:
I am trying to make it so that when a player jumps down and touches an invisible part, their “Time” goes up by 1. I have this part done already. In the first gyazo gif, it shows what I mean.
https://gyazo.com/2b5234be5de2bc4c5d476033f579a4f2
However, the problem lies when they die. Once they die, their time gets reset to 0, making them have to go up again (which is what I intend), but when the player jumps down again, it doesn’t increment by 1. It goes up by a random value + 1. I tried using booleans and many other things, but can’t seem to figure it out. Here is what I mean:
https://gyazo.com/a6d8253afe77809b7e0ac54f9131a18e
If anyone can help, that would be amazing. Here is my script for reference (if you wish to replicate or see what I did):
local region = game.Workspace:WaitForChild("Region")
local canActivate = true
local dead = false
local loop = true
local function addTime()
while true do
local players = game.Players:GetPlayers()
if #players >= 1 and not dead and canActivate and loop then
for _, player in pairs(players) do
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then continue end
local time = leaderstats:FindFirstChild("Time")
local bestTime = leaderstats:FindFirstChild("BestTime")
if not time or not bestTime then continue end
time.Value = time.Value + 1
if time.Value > bestTime.Value then
bestTime.Value = time.Value
canActivate = false
end
end
end
wait(1)
canActivate = true
end
end
local function initLeaderstats(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timeValue = Instance.new("IntValue")
timeValue.Name = "Time"
timeValue.Parent = leaderstats
local bestTimeValue = Instance.new("IntValue")
bestTimeValue.Name = "BestTime"
bestTimeValue.Parent = leaderstats
return leaderstats, timeValue, bestTimeValue
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats, timeValue, bestTimeValue = initLeaderstats(player)
player.CharacterAdded:Connect(function(char)
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
timeValue.Value = 0
dead = false
loop = false
end)
end)
end)
region.Touched:Connect(function(Hit)
local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if player and canActivate then
dead = false
loop = true
addTime(player)
end
end)