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

How do I make the script work? (edited from previous)

Asked by 8 years ago

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

3 answers

Log in to vote
0
Answered by 8 years ago
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.

0
(just so you get the notification) The script isn't printing an error but it is also not doing anything. My script matches both of yours and it's not moving the brick. DarwinYork 85 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

0
The script isn't printing an error but it is also not doing anything. My script matches both of yours and it's not moving the brick. DarwinYork 85 — 8y
Log in to vote
0
Answered by 8 years ago

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)

If this doesn't work, then I have no idea.

Answer this question