How to fix delay between mouse release and throw

I have a function in which when the mousebutton2 is released the part that’s “Grabbed” is thrown by changing its velocity. However when the mouse button is released, there’s a delay like this:

I am firing a remote event and then a server script changes the velocity. If anyone has any ideas as to how I can remove or shorten the delay. let me know

That delay seems abnormally long. It would probably be helpful to see some of your code that handles the networking aspects of this.

this is it:

local grabbedevent = game.ReplicatedStorage.ItemGrabbed
local releasedevent = game.ReplicatedStorage.ItemReleased

grabbedevent.OnServerEvent:Connect(function(plr, item)
	local val = Instance.new("BoolValue", item)
	val.Name = "IsGrabbed"
	repeat wait() until not item.Parent:FindFirstChild("WeldTo")
	item:SetNetworkOwner(plr)
end)

releasedevent.OnServerEvent:Connect(function(plr, item, throw, dir)
	if item:FindFirstChild("IsGrabbed") then
		item.IsGrabbed:Destroy()
		item:SetNetworkOwner(nil)
	end
	if throw == true then
		item.Velocity = dir
	end
end)

How about on the client side? And when is WeldTo being removed in this process?

It seems like you could probably just do all of this on the client side, you’re getting all of the information from there anyway. I would suggest that, with the exception of the explosion. I would handle the explosion and all of the work entailed on the server side and the grenade throw seems fine to be on the client side. I suspect if you’re setting the network owner then you should be able to set the velocity on the client side. Alternatively you could use a BodyMover of some sort to handle that, which should work from the client side.

There isn’t a way to reduce that delay, as the delay itself is already arbitrary. Your system is bottlenecked by the RemoteEvent and the time it takes for the signal to reach the server along with the data you’re passing through.

Do you require the server for this or are you able to move things to the client? Everything visual should be done on the client.

1 Like

I’ve tried to do that on the client side however the Networking collerates with the change in velocity and the velocity doesnt change, the grenade only gets dropped on the ground.

The WeldTo is only specific to certain objects, for example the grenade doesn’t have the “WeldTo” The items that do have the same length delay as the grenade.

Here’s the client script:

local plr = game.Players.LocalPlayer
local char = plr.Character
local grabbing = false
local grabbeditem = nil
local facing = false
local dist = 11
local userinputservice = game:GetService('UserInputService')
local mouse = plr:GetMouse()
local camera = workspace.CurrentCamera
local highlightobject = nil
camera.FieldOfView = 90


repeat wait() until plr.Character

mouse.Button1Down:Connect(function()
	if mouse.Target and mouse.Target.Parent:IsA("Model") and mouse.Target.Parent:FindFirstChild("Grabbable") then
		if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
			grabbing = true
			grabbeditem = mouse.Target.Parent.PrimaryPart
			_G.ItemToFace = grabbeditem
			game.ReplicatedStorage.ItemGrabbed:FireServer(grabbeditem)
			local bp = Instance.new("BodyPosition", grabbeditem)
			bp.D = 150
			bp.P = 2000
			grabbeditem.Parent.Outline.Transparency = 0
		end
	end
end)

mouse.Button2Down:Connect(function()
	if grabbing == true then
		facing = true
	end
end)

mouse.Button2Up:Connect(function()
	facing = false
	grabbing = false
	_G.ItemToFace = nil
	if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
		game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem, true,camera.CFrame.LookVector * 150)
		grabbeditem.Parent.Outline.Transparency = 1
		grabbeditem.BodyPosition:Destroy()
	end
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
	
	
end)

mouse.Button1Up:Connect(function()
	grabbing = false
	facing = false
	_G.ItemToFace = nil
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
		game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem, false)
		grabbeditem.Parent.Outline.Transparency = 1
		grabbeditem.BodyPosition:Destroy()
	end
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if grabbing == true and grabbeditem ~= nil then
		if (plr.Character.PrimaryPart.Position - grabbeditem.Position).Magnitude < dist then
			if facing == false then
			grabbeditem.BodyPosition.Position = ((camera.CFrame * CFrame.new(0,0,-(dist - 4))) ).Position
			elseif facing == true then
				grabbeditem.BodyPosition.Position = ((camera.CFrame * CFrame.new(2,1,-(dist - 8))) ).Position
			end
			plr.Character.RightUpperArm.RightShoulder.C0 = CFrame.new(Vector3.new(), grabbeditem.Position)
		else
			grabbing = false
			if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
				grabbeditem.BodyPosition:Destroy()
				game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
			elseif grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
				grabbeditem.BodyGyro:Destroy()
			end
		end
			else
				if mouse.Target ~= nil then
					if mouse.Target.Parent:IsA("Model") then
					if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and mouse.Target.Parent:FindFirstChild("Grabbable")  then
					if (plr.Character.PrimaryPart.Position - mouse.Target.Parent.PrimaryPart.Position).Magnitude < dist * 1.2 and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
					if highlightobject and mouse.Target ~= highlightobject then
						highlightobject.Transparency = 1
					end
					highlightobject = mouse.Target.Parent.Outline
					highlightobject.Transparency = 0
					end
				else
					if highlightobject then
						highlightobject.Transparency = 1
					end
			end
		
				else
				if highlightobject ~= nil then
				highlightobject.Transparency = 1
			end
		end
	end	
	end
end)

The explosion is seperate of these scripts so it doesnt affect that delay, everything visual except for the change in velocity while throwing is done on the client

How about using a BodyVelocity instead of setting the velocity property on the client side? I think that would work fine.

1 Like

Going to give it a shot now and i’ll update you soon on it then :+1:

1 Like

It worked and I didn’t have to use a body mover, thanks for the help!

1 Like