Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

onTouch script doesn't work as expected !?????

Hi there! I'm having a problem, when I touch this specific block, it doesn't change the color. Can anybody help me? Any help would be HELPFUL!

parent = script.Parent

function onTouch(hit)
	parent.BrickColor = "Deep orange"
	wait (1)
	parent.BrickColor = "Really black"
	wait (1)
	parent.Transparency = "0.1"
	wait (0.1)
	parent.Transparency = "0.3"
	wait (0.1)
	parent.Transparency = "0.5"
	wait (0.1)
	parent.Transparency = "0.7"
	wait (0.1)
	parent.Transparency = "0.9"
	wait (0.1)
    parent.Transparency = "1"
    parent.CanCollide = false

end

parent.Touched:connect(onTouch)

output = Workspace.Part.OnTouch:4:bad arguement #3 to 'BrickColor' (BrickColor expected, got string) ty for taking ur time to read this, ;3.

2 comments

0

Output is telling you everything, BrickColor (In your script: BrickColor.new("Deep orange") and BrickColor.new("Really black")) expected, got string ("Deep orange" and "Really black"). Also, use parent.Touched:Connect(onTouch) instead of parent.Touched:connect(onTouch)

0

I wasn't quick enough to answer this, in face I was 9 hours late

1 answer

✓ Accepted answer

That is because BrickColor takes, well, a BrickColor, and you gave it a string, thus causing this error.

You also might want to use a debounce, since due to how the engine handles collisions, multiple collisions will be registered. You should also be using loops to animate the transparency of the part.

And a tip: use local variables.

local parent = script.Parent
local orange = BrickColor.new("Deep orange") -- # constructing brickcolor
local black = BrickColor.new("Really black")
local debounce = false

local function onTouch(hit)
    if debounce then
        return -- # already touched, do not go any further
    end
    debounce = true
    parent.BrickColor = orange
    wait (1)
    parent.BrickColor = black
    wait (1)
    for i = 0.1, 1, 0.2 do
        part.Transparency = i
        -- # starting from 0.1, going to 1, in steps of 0.2, set transparency to i
        wait(0.1)
    end
    parent.CanCollide = false
    debounce = false -- # allow for code to execute again
end

parent.Touched:Connect(onTouch)

6 comments

0

I’ll let you win this one in return

0

smh

0

You know how ridiculous it is, if you don’t want to just say it. I’ll be fine, dying inside but fine

0

also you forgot that connect is deprecated, use Connect instead of connect though (not this answer)

0

ty next

This discussion is preserved as it was. New answers, comments, and votes are closed.