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

My code isn't working, what should I do?

Asked by 9 years ago

Here's my code. There is a ClickDetector.

r = false

function onClicked(playerWhoClicked)
    r = true
    if r then
        function onClicked(playerWhoClicked)
            r = false
        end
    end
end

if r then
    script.Parent.Parent.door.Transparency = 1
    script.Parent.Parent.door.CanCollide = false
else
    script.Parent.Parent.door.Transparency = 0
    script.Parent.Parent.door.CanCollide = true
end


script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
Works! Iplaydev 10 — 9y

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You don't want to redefine the function; Technically a solution like that could be made to work, but it's a bad one because it's very confusing. (For the curious, how it would look)

Instead, just say what you mean, but use an else clause:

function onClicked(playerWhoClicked)
    if not r then -- (if r is false)
        r = true
    else
        r = false
    end
end

This could be shortened to simply

function onClicked(playerWhoClicked)
    r = not r -- "Flips" r. false --> true, true --> false
end

The remainder of the code will only happen once, at the beginning of the script.

You should use a while loop to make it repeatedly act:

script.Parent.ClickDetector.MouseClick:connect(onClicked)
-- Connection line must happen *before* start of loop
while wait() do
    -- Every 1/30 seconds...
    if r then
        script.Parent.Parent.door.Transparency = 1
        script.Parent.Parent.door.CanCollide = false
    else
        script.Parent.Parent.door.Transparency = 0
        script.Parent.Parent.door.CanCollide = true
    end
end




Of course, since this the loop only changes anything when r changes, we might as well put it in the function (the only place where r changes), meaning the entire script would just be

r = false

function onClicked(playerWhoClicked)
    if not r then -- (if r is false)
        r = true
        script.Parent.Parent.door.Transparency = 1
        script.Parent.Parent.door.CanCollide = false
    else
        r = false
        script.Parent.Parent.door.Transparency = 0
        script.Parent.Parent.door.CanCollide = true
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Edit: paste didn't include the connection line (don't just blindly copy and paste, actually look at it and analyze -maybe go through the changes yourself to learn how in the future)

1
The final script just needs a connection line, that's all. Redbullusa 1580 — 9y
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Not a fan of that script's format. Possibly because the if statement will only run once.

Have it run inside of a function.

r = false

function onClicked()
    if r then
        r = false
        script.Parent.Parent.door.Transparency = 1
        script.Parent.Parent.door.CanCollide = false
    else
        r = true
        script.Parent.Parent.door.Transparency = 0
        script.Parent.Parent.door.CanCollide = true
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Log in to vote
-2
Answered by 9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Does it have a click detector?

I honestly don't know, I'm a very novice scripter, but try this

r = false

function onClicked(playerWhoClicked)
    r = true
    if r = true then
    end
end

if r = true then
    script.Parent.Parent.door.Transparency = 1
    script.Parent.Parent.door.CanCollide = false
else
    script.Parent.Parent.door.Transparency = 1
    script.Parent.Parent.door.CanCollide = true
end


script.Parent.ClickDetector.MouseClick:connect(onClicked)

Again, probably wrong.

Answer this question