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

Push sides to push block?

Inspired from Zelda, its a crate that is supposed to be able to move to the right,left,front,back.

But for some reason it only moves front, and back.

canPush = true
script.Parent.Touched:connect(function(hit)
	if canPush == true then canPush = false
	local back = Vector3.new(script.Parent.CFrame.X + script.Parent.Size.X/2, script.Parent.Size.Y, script.Parent.Size.Z)
	local front = Vector3.new(script.Parent.CFrame.X - script.Parent.Size.X/2, script.Parent.Size.Y, script.Parent.Size.Z)
	local right = Vector3.new(script.Parent.Size.X, script.Parent.Size.Y, script.Parent.CFrame.Z - script.Parent.Size.X/2)
	local left = Vector3.new(script.Parent.Size.X, script.Parent.Size.Y, script.Parent.CFrame.Z + script.Parent.Size.X/2)
	local frontmag = (front - hit.Position).magnitude
	local backmag = (back - hit.Position).magnitude
	local leftmag = (left - hit.Position).magnitude	
	local rightmag = (right - hit.Position).magnitude
	if frontmag < backmag and frontmag < rightmag and frontmag < leftmag then
		for i = 1, 40 do
		wait()
		script.Parent.CFrame = script.Parent.CFrame - Vector3.new(-0.1, 0, 0)
		end
	elseif backmag < frontmag and backmag < rightmag and backmag < leftmag then
		for i = 1, 40 do
		wait()
		script.Parent.CFrame = script.Parent.CFrame + Vector3.new(-0.1, 0, 0)
		end
	elseif rightmag < frontmag and rightmag < backmag and rightmag < leftmag then
		for i = 1, 40 do
		wait()
		script.Parent.CFrame = script.Parent.CFrame - Vector3.new(0, 0, -0.1)
		end
	elseif leftmag < frontmag and leftmag < backmag and leftmag < rightmag then
		for i = 1, 40 do
		wait()
		script.Parent.CFrame = script.Parent.CFrame + Vector3.new(0, 0, -0.1)
		end
		end
	wait(1)
	canPush = true
	end
end)

1 answer

✓ Accepted answer

Take a look at your for loops. Two of them have the same increments, which means the box will only ever be able to move two directions. Change two of the for loops from negative to positive respective to their directions.

I would suggest a different method than magnitudes though, because it's not very reliable. Try using raycasting instead.

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