This script and the loops work successfully with no error but the actual part in the workspace doesn't move, why? My guess is that I don't know how to add to CFrames.
detector = script.Parent gate = game.Workspace.Gate open = false position = gate.CFrame detector.MouseClick:Connect(function() if open == false then detector.MaxActivationDistance = 0 for i = 1,17 do print("moved") position = position + Vector3.new(0,1,0) wait(0.5) end detector.MaxActivationDistance = 15 open = true else detector.MaxActivationDistance = 0 for i = 1,17 do print("moved") position = position - Vector3.new(0,1,0) wait(0.5) end open = false detector.MaxActivationDistance = 15 end end)
I wasn't sure if your gate was a model or a part so I showed how to do both:
To add/subtract from a CFrame's position, you'd want to multiply the current CFrame by another CFrame value. The second CFrame value is amount you want to offset your part.
I changed the name of your position variable to be gateCFrame. The code to move the gateOver would be the following:
adding to position
gate.CFrame= gate.CFrame* CFrame.new(Vector3.new(0,1,0))
subtracting from position:
gate.CFrame= gate.CFrame* CFrame.new(Vector3.new(0,1,0))
The whole script would be:
detector = script.Parent gate = game.Workspace.Gate open = false detector.MouseClick:Connect(function() if open == false then detector.MaxActivationDistance = 0 for i = 1,17 do print("moved") gate.CFrame= gate.CFrame* CFrame.new(Vector3.new(0,1,0)) wait(0.5) end detector.MaxActivationDistance = 15 open = true else detector.MaxActivationDistance = 0 for i = 1,17 do print("moved") gate.CFrame= gate.CFrame* CFrame.new(-Vector3.new(0,1,0)) wait(0.5) end open = false detector.MaxActivationDistance = 15 end end)
You can't change the cframe of a model directly since models don't have a cframe property. Instead, we need to give gate a primary part by changing the primary part property of the Gate model.
Instead of the above code where we set the CFrame of the gate part, we're going to set the CFrame of the primary part inside of the model. To do that we'd say:
Adding to position:
gate:SetPrimaryPartCFrame(gate.PrimaryPart.CFrame * CFrame.new(Vector3.new(0,1,0)))
Subtracting from position: (make the vector 3 a negative so one is now -1)
gate:SetPrimaryPartCFrame(gate.PrimaryPart.CFrame * CFrame.new(-Vector3.new(0,1,0)))