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 10 years ago

Here's my code. There is a ClickDetector.

01r = false
02 
03function onClicked(playerWhoClicked)
04    r = true
05    if r then
06        function onClicked(playerWhoClicked)
07            r = false
08        end
09    end
10end
11 
12if r then
13    script.Parent.Parent.door.Transparency = 1
14    script.Parent.Parent.door.CanCollide = false
15else
View all 21 lines...
0
Works! Iplaydev 10 — 10y

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 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:

1function onClicked(playerWhoClicked)
2    if not r then -- (if r is false)
3        r = true
4    else
5        r = false
6    end
7end

This could be shortened to simply

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

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:

01script.Parent.ClickDetector.MouseClick:connect(onClicked)
02-- Connection line must happen *before* start of loop
03while wait() do
04    -- Every 1/30 seconds...
05    if r then
06        script.Parent.Parent.door.Transparency = 1
07        script.Parent.Parent.door.CanCollide = false
08    else
09        script.Parent.Parent.door.Transparency = 0
10        script.Parent.Parent.door.CanCollide = true
11    end
12end




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

01r = false
02 
03function onClicked(playerWhoClicked)
04    if not r then -- (if r is false)
05        r = true
06        script.Parent.Parent.door.Transparency = 1
07        script.Parent.Parent.door.CanCollide = false
08    else
09        r = false
10        script.Parent.Parent.door.Transparency = 0
11        script.Parent.Parent.door.CanCollide = true
12    end
13end
14 
15script.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 — 10y
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
10 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.

01r = false
02 
03function onClicked()
04    if r then
05        r = false
06        script.Parent.Parent.door.Transparency = 1
07        script.Parent.Parent.door.CanCollide = false
08    else
09        r = true
10        script.Parent.Parent.door.Transparency = 0
11        script.Parent.Parent.door.CanCollide = true
12    end
13end
14 
15script.Parent.ClickDetector.MouseClick:connect(onClicked)
Log in to vote
-2
Answered by 10 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

01r = false
02 
03function onClicked(playerWhoClicked)
04    r = true
05    if r = true then
06    end
07end
08 
09if r = true then
10    script.Parent.Parent.door.Transparency = 1
11    script.Parent.Parent.door.CanCollide = false
12else
13    script.Parent.Parent.door.Transparency = 1
14    script.Parent.Parent.door.CanCollide = true
15end
16 
17 
18script.Parent.ClickDetector.MouseClick:connect(onClicked)

Again, probably wrong.

Answer this question