Here's my code:
local part = script.Parent local w = workspace local door = w.ddd script.Parent.ClickDetector.MouseClick:connect(function() if door.CFrame == CFrame.new(22.37, 5.115, -37.9) then door.CFrame = door.CFrame * (CFrame.new(25, 10, -40)) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = door.CFrame * (CFrame.new(22.37, 5.115, -37.9)) end end)
there is no error in the output but the brick is not moving.
the current position of the brick is 22.37, 5.115, -37.9 as in the script and it doesnt move
script.Parent.ClickDetector.MouseClick:connect(function(onClick) if door.CFrame == CFrame.new(22.37, 5.115, -37.9) then door.CFrame = door.CFrame * (CFrame.new(25, 10, -40)) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = door.CFrame* (CFrame.new(22.37, 5.115, -37.9) end end)
You attempted to compare a CFrame to 3 values. It doesn't work like that. We want to define those 3 values as a CFrame to compare it to another CFrame. To do this, we shall use CFrame.new so the 3 values will be defined as a CFrame.
I believe you also need to add an extra )
to the end of line 6:
script.Parent.ClickDetector.MouseClick:connect(function(onClick) if door.CFrame == CFrame.new(22.37, 5.115, -37.9) then door.CFrame = door.CFrame * (CFrame.new(25, 10, -40)) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = door.CFrame * (CFrame.new(22.37, 5.115, -37.9)) end end)
Also, note the indentation on the if block. It's not required, but generally good practice.
EDIT: I'm going to assume you're trying to make door
move from one point to another and vice versa. In that case, you should change it to:
script.Parent.ClickDetector.MouseClick:connect(function(onClick) if door.CFrame == CFrame.new(22.37, 5.115, -37.9) then door.CFrame = CFrame.new(25, 10, -40) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = CFrame.new(22.37, 5.115, -37.9) end end)
The previous script would add to the door
's position; this one sets it directly to that position.
Mr. CFrame, meet a bunch of numbers.
As you said in Chat, it is not moving, at all.
That is because you are comparing CFrame
with a bunch of Numbers
.
First, let's discuss:
CFrames are not a bunch of numbers, but rather userdata.
Userdata
is data structures.
Now that we got that out of the way, why does your script not work?
It's because, like I said, CFrames are not numbers.
local part = script.Parent local w = workspace local door = w.ddd script.Parent.ClickDetector.MouseClick:connect(function() if door.CFrame == CFrame.new(22.37, 5.115, -37.9) then door.CFrame = door.CFrame * (CFrame.new(25, 10, -40)) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = door.CFrame * (CFrame.new(22.37, 5.115, -37.9)) end end)
In your code, you are seen comparing CFrames, or are you?
You are actually comparing CFrames
to Numbers
print(CFrame.new(1337, 360, 420) == (1337,360,420)) --false print(CFrame.new(1337,360,420) == CFrame.new(1337,360,420)) -- true
Why does the second one return true? Is it because it's longer?
No, it's actually because we are comparing the correct data types now.
We are comparing CFrames with CFrames!
Now let's fix that code of yours.
local part = script.Parent local w = workspace local door = w.ddd script.Parent.ClickDetector.MouseClick:connect(function() if door.CFrame == CFrame.new(22.37, 5.115, -37.9) then door.CFrame = door.CFrame * (CFrame.new(25, 10, -40)) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = door.CFrame * (CFrame.new(22.37, 5.115, -37.9)) end end)
Well, it turns out your edit fixed that error, crap
From what I see the only error here is that Roblox's program cannot compare Floats
correctly.
Floats are numbers, with decimals, such as 123.51241
With computers, it is hard to compare floats, because they are specific.
To fix that, round all your CFrames into wholes like
local part = script.Parent local w = workspace local door = w.ddd script.Parent.ClickDetector.MouseClick:connect(function() if door.CFrame == CFrame.new(22,5 , -37) then door.CFrame = (CFrame.new(25, 10, -40)) elseif door.CFrame == CFrame.new(25, 10, -40) then door.CFrame = (CFrame.new(22, 5 -37)) end end)