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

CFrame.new from Vector3.Value Fix?

Asked by
Moxeh 84
9 years ago

How do I fix this? Output below...

Workspace.Switchboard.Audio Level Design.Regular Sliders.Sl:5: bad argument #3 to 'Position' (Vector3 expected, got userdata)

function onClicked()
local on = script.Parent.Vector3.On
local off = script.Parent.Vector3.Off
    if script.Parent.Position == off.Value then
        script.Parent.Position = CFrame.new(on.Value)
        end
    if script.Parent.Position == on.Value then
        script.Parent.Position = CFrame.new(off.Value)
    end
    end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
Look at my script again, your script is different. Take the off value and stuff and replace "0,0,0" in "CFrame.new(0,0,0) --[[Off value]]". Same with the On Value one. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

To change the position you need to use a Vector3 value, to change it with CFrame you need to change the part's CFrame. e.g.

workspace.Part.Position = Vector3.new(0,0,0)
--Both work, but CFrame can clip through other objects.
workspace.Part.CFrame = CFrame.new()

Now with that knowledge lets fix your script.

function onClicked()
    if script.Parent.CFrame == CFrame.new(0,0,0)--[[Off Value]] then
        script.Parent.CFrame = CFrame.new(on.Value)
        end
    if script.Parent.CFrame == CFrame.new(0,1,0) --[[On Value.]] then
        script.Parent.CFrame = CFrame.new(off.Value)
    end
    end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Please edit the parts next to the green comments.


Hope it helps.

0
It doesn't do anything, not even an error. function onClicked() local on = script.Parent.Vector3.On local off = script.Parent.Vector3.Off if script.Parent.CFrame == CFrame.new(off.Value) then script.Parent.CFrame = CFrame.new(on.Value) end if script.Parent.CFrame == CFrame.new(on.Value) then script.Parent.CFrame = CFrame.new(off.Value) end end script.Parent.ClickDetector.MouseClick:conne Moxeh 84 — 9y
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You're setting a Position (a Vector3) to a CFrame. CFrames aren't Vector3s, so you can't do that.

To get the position of a CFrame, use its .p property -- read the wiki.

However, it would be better to set the CFrame, rather than set the Position, because Position behaves rather poorly.

Thus the best solution would simply be

function onClicked()
    local on = script.Parent.Vector3.On
    local off = script.Parent.Vector3.Off
    if script.Parent.Position == off.Value then
        script.Parent.CFrame = CFrame.new(on.Value)
    end
    if script.Parent.Position == on.Value then
        script.Parent.CFrame = CFrame.new(off.Value)
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Remember to tab your code correctly

Of course, it's not a good idea to compare the Positions, or to compare to on and off when they're opposites. Use a boolean variable:

-- Something like this:

local on = false -- Or true, whichever it STARTS as

function onClicked()
    local go
    if on then
        go = script.Parent.Vector3.On
    else
        go = script.Parent.Vector3.Off
    end
    script.Parent.CFrame = CFrame.new(go.Value)
    on = not on -- true --> false --> true
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

0
Didn't work :/ no error but doesn't move... Moxeh 84 — 9y

Answer this question