I am making a sort of checkpoint system, where the position is updated everytime a player has hit a checkpoint, but how would I do this? My first thought was using a table, but I have no idea how to use one. I checked here but that doesn’t give me an idea on how to use it in this sort of situation. Here is the code:
local finish = false
if script.Parent.Name == "finish" then
finish = true
end
num = 0
local num = tonumber(script.Parent.Name)
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
--probably here
local name = hit.Parent.Name
if finish == true then
game.Players[hit.Parent.Name].tempstats.finished.Value = true
game.Players[hit.Parent.Name].tempstats.checkpoint.Value = 0
else
if game.Players[hit.Parent.Name].tempstats.checkpoint.Value == num-1 then
game.Players[hit.Parent.Name].tempstats.checkpoint.Value = num
end
end
end
end)
When the player hits the part, check if his name isnt in the table. If it is not, then insert his name into that table. And make an if statement that will run the touching event for only players who havent touched it yet( by checking their name in the table).
You could use a for loop to check all checkpoints from one script:
for _, v in ipairs(checkpoints:GetChildren()) do -- checks all parts simultaneously
v.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") == nil then return end -- if hit isn't a player it will cancel the function
local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
if tonumber(v.Name) <= plr.tempstats.checkpoint.Value then return end -- assuming the checkpoint names are 1, 2, 3, ...
plr.tempstats.checkpoint.Value = tonumber(v.Name)
end)
end
If you need to put anything else alongside the loop,
you could wrap it in a coroutine.
I’m assuming you can customise the code further to your liking.
Hopefully this helped!