First of all, I don't see why the
game:WaitForChild("Players").PlayerAdded:Connect(function()
is necessary. The callback function will run every time a player joins, which means that a new checkpoint.Touched listener will be created every time a player joins, slowing down your code and creating potential bugs. Simply removing the line and only creating one listener seems sufficient.
Secondly, your statement on line 5
if game.Players:GetPlayerFromCharacter(hit.Parent.Checkpoints.Checkpoint1) then
is checking whether or not whatever you're touching has a child called Checkpoint1 and that Checkpoint1 is a player. Because checkpoint1 isn't a player, the if statement will always be skipped and your code will never run. Instead, check hit.Parent
This should make your code work. However, ideally you would want to stop checking collisions after the part has been touched, because nothing is going to change after that. Therefore, assign checkpoint.Touched to a variable and call Disconnect on it when a player touches it.
In the end (assuming your variables are all correct and the script is where it should be) your code should look like this:
local checkpoint = game.Workspace.Checkpoints.Checkpoint1
local connection = checkpoint.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
wait(15)
script.Parent.CanCollide = false
script.Parent.BrickColor = BrickColor.new("Lime green")
checkpoint.BrickColor = BrickColor.new("Lime green")
connection:Disconnect()
end
end)
1 comment
When you use it, does it say anything in the returning console? If so, what does it say? Or does it just not work?