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

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

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)

1 comment

0

Works!

3 answers

✓ Accepted answer

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 comment

1

The final script just needs a connection line, that's all.

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)

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.

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