There are a few problems with this.
Begin by tabbing your code correctly
You are defining onActivated twice.
It's usually bad o define functions inside if or for or while. In this case, that is causing a problem, especially because
Your two if statements AREN'T such that if one happens, the other doesn't:
- The first one may succeed and the second fail (use
A)
- The second may succeed and the first may fail (use
B)
- Both may succeed (first use
A, then obliterate that and use B)
- Neither may succeed (the cause of your error).
You defined Tool but barely use it! That will shorten your code a lot. But the lowercase tool would be preferable in ROBLOX's naming system (and the remainder of your names).
At the same time, it makes no sense at all to refer to Lighting like that. Use game.Lighting. Besides that, you should use ReplicatedStorage for storing objects, not the Lighting.
So ABSOLUTELY first, we need to use else instead of having two ifs:
math.random(1, 2);
local tool = script.Parent;
wait(2);
if math.random == 1 then
function onActivated()
local rc = tool.Parent.Parent.Parent.Lighting.A:Clone();
rc.Parent = tool.Parent;
end
else
function onActivated()
local rc = tool.Parent.Parent.Parent.Lighting.B:Clone();
rc.Parent = tool.Parent;
end
end
Tool.Activated:connect(onActivated);
This will prevent the error. However, it's still wrong.
math.random is a function. It won't ever be 1. You have to call it, like you did on the first line. But the call on the first line doesn't do anything since you didn't use the value it returned.
You should instead say
local tool = script.Parent
wait(2);
if math.random(2) == 1 then
function onActivated()
local rc = tool.Parent.Parent.Parent.Lighting.A:Clone();
rc.Parent = tool.Parent;
end
else
function onActivated()
local rc = tool.Parent.Parent.Parent.Lighting.B:Clone();
rc.Parent = tool.Parent;
end
end
Tool.Activated:connect(onActivated);
As I said earlier, though, you shouldn't be defining the function twice.
You should define it once.
If you meant that, it will decide when created either A or B to use forever, then you would want to do this:
local tool = script.Parent
local rc
if math.random(2) == 1 then
rc = game.Lighting.A
else
rc = game.Lighting.B
end
function onActivated()
rc:Clone().Parent = tool.Parent;
end
Tool.Activated:connect(onActivated);
If you instead wanted to make it random each time the tool is activated, you would move the if into the function:
local tool = script.Parent
function onActivated()
if math.random(2) == 1 then
game.Lighting.A:Clone().Parent = tool.Parent
else
game.Lighting.B:Clone().Parent = tool.Parent
end
end
Tool.Activated:connect(onActivated);
3 comments
What on earth. I just submitted an answer and it didn't go through.
XD can you re-post it please? XD
I did. :P