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

Script somewhat works?

Asked by 9 years ago

Please forgive me if this seems like something simple, I am currently a beginner in learning Lua. Anyways, I was trying to make a script in which when you are touching the brick (In my case when I am standing on top) the brick turns a different color (Blue), and when you step off of the brick it turns back to red. It does what it's supposed to do, however, when ever I walk across the brick it flashes between red and blue, kind of like I am not touching it when walking. I don't get it. If you don't know what I mean here is the script:

function onTouch() script.Parent.BrickColor = BrickColor.new("Bright blue") wait(1) end

function offTouch() script.Parent.BrickColor = BrickColor.new("Bright red") wait(1) end

script.Parent.Touched:connect(onTouch) script.Parent.TouchEnded:connect(offTouch)

Could someone help, and if possible maybe there is a more efficient way of doing this?

1 answer

Log in to vote
0
Answered by
Xetrax 85
9 years ago

The only more efficient way I can think of goes like this:

script.Parent.Touched:connect(function()
    script.Parent.BrickColor = BrickColor.new("Bright blue")
    wait(1)
end)
script.Parent.TouchEnded:connect(function()
    script.Parent.BrickColor = BrickColor.new("Bright red")
    wait(1)
end)

Shorter and should work a teensy bit faster, its just embedding a function within an event. Hope that helps!

0
I appreciate making it more efficient but it still does the 'flashing' thing. It's weird, when I walk on top of the brick, not jumping or anything, it flashes colors instead of staying one color. It's like I'm barely coming off the ground when I walk. Any other ideas? ghostblocks 74 — 9y
0
Its because the event Touched fires continuously, only once, then TouchEnded fires, and back and forth they go, theres no way to really fix this, except put an extensive wait between the two. Xetrax 85 — 9y
0
 Why not connect TouchEnded after the wait(1) on the Touched function in Xetrax's script? Or, why not use debounce? Spongocardo 1991 — 9y
0
@Spongocardo, how would I do that? ghostblocks 74 — 9y
Ad

Answer this question