Problems With Syncing Character Velocity

Hey Roblox Devs! I’m currently having tons of trouble with syncing player velocity to the server. I have been working on it for so freaking long at it’s making me loose my mind haha. So far I’ve tried using RemoteFunctions (have no idea how those work so couldn’t do it with those) and RemoteEvents (too much network latency in sending the Vector3) here’s the main part of code that I’m using right now.

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game.Players

local powerUpsFolder = workspace:WaitForChild("PowerUps")
local powerUpsChildren = powerUpsFolder:GetChildren()

local getVelocityEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("GetVelocity")

local fracturedSphere = script:WaitForChild("FracturedSphere")
local torsoVel = Vector3.zero

getVelocityEvent.OnServerEvent:Connect(function(player, velocity)
	torsoVel = velocity
	print(torsoVel)
end)

for i, v in pairs(powerUpsChildren) do
	if v:IsA("BasePart") then
		v.CanCollide = false
		local fracturedSphereClone = fracturedSphere:Clone()
		for i, fragment in pairs(fracturedSphereClone:GetChildren()) do
			if fragment:IsA("BasePart") then
				fragment.Transparency = 1
				fragment.CanCollide = false
				fragment.CustomPhysicalProperties = PhysicalProperties.new(0.01,0.1,1)
				fragment.CanTouch = false
			end
		end

		fracturedSphereClone:PivotTo(CFrame.new(v.Position))
		fracturedSphereClone.Parent = powerUpsFolder

		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				getVelocityEvent:FireClient(Players:GetPlayerFromCharacter(hit.Parent))
				repeat wait() until torsoVel ~= Vector3.new(0,0,0)
				for i, fragment in pairs(fracturedSphereClone:GetChildren()) do
					if fragment:IsA("BasePart") then
						fragment.Transparency = 0
						fragment.Anchored = false
						fragment.AssemblyAngularVelocity = Vector3.new(math.random(-20,20),math.random(-20,20),math.random(-20,20))
						fragment.Velocity = ((fragment.Position - v.Position).unit * 50) + Vector3.new(0,100,0) + (torsoVel) -- Apply velocity away from the center
					end
				end
				torsoVel = Vector3.zero

				v:Destroy()
			end
		end)
	end
end

Local Script:

getVelocityEvent.OnClientEvent:Connect(function()
	getVelocityEvent:FireServer(torso.Velocity)
end)

This is the version with the RemoteEvent but like I said it doesn’t work very well. Thanks so much for reading this, please if you have any suggestions or are able to help please don’t hesitate to send a reply! Thanks!

2 Things:

  1. You should look into RemoteFunctions, they let you invoke the client and get data back without having to do that remote loop shenanigans.

  2. You don’t even need to get the velocity from the client, pretty much everything about your character is automatically replicated to the server thanks to the way humanoids work. You should just be able to get the velocity straight from the torso.

local Player = (...)
local PlayerTorso: BasePart = Player.Character:FindFirstChild("Torso")

if not PlayerTorso then
   return
end

local Velocity = PlayerTorso.AssemblyLinearVelocity
local Speed = Velocity.Magnitude
1 Like

dude i wish i could, but if i try printing the velocity of the player while they are moving it always just returns Vector3.new(0,0,0). Also, when i tried remoteFunctions it just didn’t seem to work how i needed it to for some reason, unless you may have an example?

It’s pretty easy, just connect your function on the client, and have it return a value. Then, you can set a variable to :InvokeClient() and it should come out the other end.

--Server
local RemoteFunction = (...)

local PlayerVelocity = RemoteFunction:InvokeClient()
--Client
local RemoteFunction = (...)

RemoteFunction.OnClientInvoke = function()
    return Torso.Velocity
end)
1 Like

YESS!! thank you so much, that’s exactly what i needed even though it was so simple haha, thanks for your help!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.