Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am trying to make a part move in the workspace with a loop, but the position doesn't change, why?

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)

1 answer

✓ Accepted answer

I wasn't sure if your gate was a model or a part so I showed how to do both:

#If Gate is one part do the following:

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)

#If Gate is a model, do the following: 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)))

7 comments

0

Well I selected your answer too early it seems. The gate is a union, but I tested this on a regular part as well and it got the same result. The loops are working as intended but the part is not actually moving in-game.

0

Try moving it without any of the other code to see if it's an issue with the loops or to see if it's an issue with the logic

0

I.e. try: gateCFrame = gateCFrame * CFrame.new(Vector3.new(0,1,0))

0

wait im dumb i'll change the answer

1

Should be gate.CFrame= gate.CFrame* CFrame.new(Vector3.new(0,1,0)). You were initially just changing the CFrame in the gateCFrame variable, not changing CFrame itself

0

This worked, thanks.

0

im glad

This discussion is preserved as it was. New answers, comments, and votes are closed.