Local and Server problem

I have been trying to program a zombie that attacks your cloned character in a characters folder, but when I tested it out, the zombies seemed to be attacking my character clone from pretty far away but when I went into server view, they were way closer than it looked in the client view. Here’s my script:

local bestDistance = nil
local target = nil
local CharactersFolder = game.Workspace.Characters
local Pathfinding = game:GetService("PathfindingService")
local Debounce = false
local touch = false

script.Parent:WaitForChild("HitRadious").Touched:Connect(function(part)
	--touch = true
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum and Debounce == false then
		if hum.Health > 0 and hum.Parent.Name ~= "Zombie" then
			touch = true
			Debounce = true
			--hum:TakeDamage(script.Parent.Damage.Value)
			--hum.WalkSpeed = 0
			script.Parent.HumanoidRootPart.Anchored = true
			--hum:TakeDamage(10)
			script.Parent.Humanoid:LoadAnimation(script.Parent.Attack):Play()
			wait(0.75)
			Debounce = false
			if not(script.Parent.Humanoid.Health <= 0) then
				script.Parent.HumanoidRootPart.Anchored = false
			end
		end
	end
end)

script.Parent.HitRadious.TouchEnded:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		touch = false
	end
end)

while true do
	target = nil
	bestDistance = nil
	for i,v in pairs(CharactersFolder:GetChildren()) do
		local magnitude = (v:WaitForChild("HumanoidRootPart").Position-script.Parent.HumanoidRootPart.Position).Magnitude
		if magnitude < script.Parent.Range.Value and not(v.Humanoid.Health <= 0) then
			if bestDistance then
				if magnitude < bestDistance then
					bestDistance = magnitude
					target = v
				end
			else
				bestDistance = magnitude
				target = v
			end
		end
	end
	
	if target then
		local path = Pathfinding:CreatePath()
		path:ComputeAsync(script.Parent.HumanoidRootPart.Position,target.HumanoidRootPart.Position)
		local waypoints = path:GetWaypoints()
		for i, waypoint in pairs(waypoints) do
			script.Parent.Humanoid:MoveTo(waypoint.Position)
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				script.Parent.Humanoid.Jump = true
			end
			script.Parent.Humanoid.MoveToFinished:Wait(2)
			if i == 2 then
				break
			end
		end
	else
		wait()
	end
end

there are no local scripts associated with the zombies. Why is this happening?

Here are some pictures:
In client view:

In server view:

so basically the issue you are experiencing is latency. when you move on the client it has to tell the server that your position updated. This action takes time depending on how fast your internet connection is. To fix it you could make a clone zombie on client and predict where the zombie actually is for you. All you would need to do is calculate your ping and get the velocity of the zombie. Ping (in seconds) = T and Velocity = V you can use the equation D = V*T to get the offset of the zombie and predict where it will really be on the client.

3 Likes

How do I get the ping of a player and the velocity of a moving NPC? Also, how can I use that to move it to the RIGHT place?

1 Like

you would you client server model to make a replicated dummy on the client and just make its position be the offset distance. To get the velocity of the dummy you can just use V = D/T or if you want just use walkspeed as the velocity. I believe there is a event on humanoid objects that you can use to determine velocity. for ping you could do something like.

--Client
Event:FireServer(tick())

--Server 
Event.OnServerEvent:Connect(function(Player, Time)
    local Ping = tick()-Time
end)
4 Likes