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

i make this basic script that change the part and it didnt worked why?

Asked by 6 years ago

i make this basic script

block = script.Parent
on = true


block.Touched:connect(function()
    if on == true then
        block.BrickColor = BrickColor.new('Really red')
        on = false
        wait(1)
    else
        block.BrickColor = BrickColor.new('Lime green')
    end
end)

but as soon i started the game the part is already changed to red i dont even touched the part but when i touched it, just change to green and cant change back to red i'm still learning on scripting

1 answer

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

Nice work and welcome to scripting!

The reason why the part starts red is because the part has probably already touched another part that is NOT you. This could be the platform under the part or a part that's already touching it. This happens because the Touched event fires when any other part touches that part. To fix this, you will need to check if the part that touched it is one of your own character parts.

local block = script.Parent
on = true

--Inside the function() we can enter a parameter called "hit" to define the part that touches.
block.Touched:connect(function(hit)

    --We can wrap all this inside another if statement to check if "hit" is a part inside your character by finding the humanoid in the parent of "hit"
    if hit.Parent:FindFirstChild("Humanoid") then
        if on == true then

            block.BrickColor = BrickColor.new('Really red')
            wait(1)

            on = false
        else
            block.BrickColor = BrickColor.new('Lime green')
        end
    end
end)

0
oh okay thanks for the explanation now i know what is going on the part is touching the Baseplate thanks :) ReyLaFreeze 26 — 6y
Ad

Answer this question