Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

script to wait then change brick properties isn't working?

Asked by
zomspi 541 Moderation Voter
5 years ago

I am trying to make a script so when you hit a part it starts a countdown of 15 seconds and then changes the properties of my part.

local checkpoint = game.Workspace.Checkpoints.Checkpoint1

game:WaitForChild("Players").PlayerAdded:Connect(function()
     checkpoint.Touched:Connect(function(hit)
        if game.Players:GetPlayerFromCharacter(hit.Parent.Checkpoints.Checkpoint1) then
    wait(15)

    script.Parent.CanCollide = false
    script.Parent.BrickColor = BrickColor.new("Lime green") 
    game.Workspace.Checkpoints.Checkpoint1.BrickColor = BrickColor.new("Lime green") 
end
    end)
    end)
0
When you use it, does it say anything in the returning console? If so, what does it say? Or does it just not work? doublevisions 3 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago

I don't see why you have to make the script so long. As you can just type this

script.Parent.Touched:Connect(function()
    wait(15) -- waits 15 seconds before the changes
    script.Parent.CanCollide = false -- changes scripts parent cancollide off/false
    script.Parent.BrickColor = BrickColor.new("Lime green") -- changes color of scripts parent
    game.Workspace.Checkpoints.Checkpoint1.BrickColor = BrickColor.new("Lime green") -- changes color of checkpoint1
end)

I tested it and it works. REMINDER: Always anchor them.

Answer this question