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

How does handle movement work, and what am I doing wrong?

First off, I am using a position handles object (http://wiki.roblox.com/index.php?title=API:Class/Handles) to move blocks around on the client in a game.

The following code allows users to move blocks around by means of a position handle (the variable hnd_trns, which stands for handle translate), however there is a really annoying problem I am faced with which I will explain after the code.

local r = game.ReplicatedStorage.r; --remote event
local hnd_trns = plr.PlayerGui:WaitForChild("Position_Handle");
local debounce = false;
local last_drag_dist = 0;

local translate_movement = {
	[Enum.NormalId.Front] = function(obj, delta)
		obj.CFrame = obj.CFrame + (obj.CFrame.lookVector * delta);
	end;
	[Enum.NormalId.Back] = function(obj, delta)
		obj.CFrame = obj.CFrame - (obj.CFrame.lookVector * delta);
	end;
	[Enum.NormalId.Top] = function(obj, delta)
		obj.CFrame = obj.CFrame + (obj.CFrame.upVector * delta);
	end;
	[Enum.NormalId.Bottom] = function(obj, delta)
		obj.CFrame = obj.CFrame - (obj.CFrame.upVector * delta);
	end;
	[Enum.NormalId.Right] = function(obj, delta)
		obj.CFrame = obj.CFrame + (obj.CFrame.rightVector * delta);
	end;
	[Enum.NormalId.Left] = function(obj, delta)
		obj.CFrame = obj.CFrame - (obj.CFrame.rightVector * delta);
	end;
};

hnd_trns.MouseDrag:connect(function(face, dist)
	if not debounce then
		debounce = true;
		local delta = (dist - (dist % 0.5)) - (last_drag_dist - (last_drag_dist % 0.5)); --move blocks by 1/2 of a stud
		translate_movement[face](hnd_trns.Adornee, delta);
		last_drag_dist = dist;
		debounce = false;
	end
end);

hnd_trns.MouseButton1Up:connect(function(face)
	last_drag_dist = 0;
	r:FireServer{"UPDATE_CFRAME", hnd_trns.Adornee, hnd_trns.Adornee.CFrame}; --update the blocks CFrame over the server
end)

this code works great, however occasionally whenver a user moves a block, then lets go of the handle (MouseButton1Up), the block will move 1/2 of a stud in a seemingly random direction. This often happens when a user tries to place a block right beside another block, however when a user tries to enter the mouse position mode, I make sure over the server that all blocks are CanCollide false so that a user can easily move blocks inside of each other without any issues.

there is no code that changes the position of the given block in the MouseButton1Up event function. All that "UPDATE_CFRAME" does is tell the server to update the blocks CFrame to the specified CFrame (in this case the block's local CFrame over the client).

all the server code does is say obj.CFrame = new_cframe

So with that, I am wondering what is the cause of this problem? Does it have anything to do with my delta calculation? Does it have anything to do with my iteration? Am I forgetting to do something important, or am I doing something that is completely not necessary?

thanks

1 answer

I fixed the silly bug.

It turns out there was nothing wrong with my method of movement.

The cause of the problem was server latency, and some lazy coding in another part of my script.

:D

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