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)
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.
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)