NPC Flying off the map

Hello.
So I’m making a referee and I’m using NPC’s to represent the chains crew. I’ve ran into a problem where once the NPC reaches the destination it just decides to fly away. It happens after a couple of attempts. Please help me fix this.

Clip:
https://medal.tv/games/roblox/clips/DfW3r3_wX0SZi/d1337o2rGxC4?invite=cr-MSxjanYsMTM0MTAwOTgs

Code:

function MoveLos(Z)
	local Unit = Pads.TrackPad3.CFrame - Pads.TrackPad3.CFrame.Position
	Pads.TrackPad3.CFrame = CFrame.new(Pads.TrackPad3.Position.X,Pads.TrackPad3.Position.Y,Z) * Unit
	Models.Referee3.Humanoid:LoadAnimation(Replicated:FindFirstChild('Game').Anims:FindFirstChild('Referee').Chains.Grab):Play()
	Models.Referee3.Humanoid:LoadAnimation(Replicated:FindFirstChild('Game').Anims:FindFirstChild('Referee').Chains.Hold):Play()
	Models.Referee3.Humanoid.WalkToPoint = Vector3.new(Pads.TrackPad3.Position.X,Pads.TrackPad3.Position.Y,Pads.TrackPad3.Position.Z)
	Models.Referee3.Humanoid.MoveToFinished:Connect(function()
		Module.CharFace(Models.Referee3)
	end)
end

Module:

UT.CharFace = function(Model)
	if Model:IsA('Model') then
		local TI = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
		local TG = {Orientation = Vector3.new(0,-90,0)}
		local TT = TweenService:Create(Model.PrimaryPart, TI, TG):Play()
	end
end

I’m not exactly sure, but this probably has something to do with you messing with the Orientation itself. Try changing the CFrame.

UT.CharFace = function(Model)
	if Model:IsA('Model') then
		local TI = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
		local TG = {CFrame = CFrame.new(Model.PrimaryPart.Position) * CFrame.Angles(0,math.rad(-90),0)}
		local TT = TweenService:Create(Model.PrimaryPart, TI, TG):Play()
	end
end

If it still flies away, try to anchor and unanchor after.

UT.CharFace = function(Model)
	if Model:IsA('Model') then
		local TI = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
		local TG = {CFrame = CFrame.new(Model.PrimaryPart.Position) * CFrame.Angles(0,math.rad(-90),0)}
		Model.PrimaryPart.Anchored = true
		local TT = TweenService:Create(Model.PrimaryPart, TI, TG):Play()
		TT.Finished:Wait()
		Model.PrimaryPart.Anchored = false
	end
end

Also, I didn’t think about this until now, but some NPC’s tend to have a larger primary parts after moving from their original position, so it might be rotating around that while the primary part stays in the same place.

1 Like

It stopped flying thank you. But now for sum reason it like teleports the character before tweening it towards the other sideline. Is this a MoveToPoint issue or?

Change this

Model.PrimaryPart.Position

to this

Model.PrimaryPart.CFrame.Position

Not sure if it’s the problem, but it does sometimes mess up due to that.

I’ve also ran into a problem where. If the humanoid is traveling too far it just decides to give up and stop moving. Causing me to press the button twice making it 2 trips to get to its destination. How could I fix that?

Or just like in general fix the laggy look of when it starts to walk and stops to walk

Oh, I had made a script like this for a TDS game since I also ran into that problem.

repeat
	model.Humanoid:MoveTo(pos)
	local reached = model.Humanoid.MoveToFinished:Wait()
until reached

So, with your code, it should look like:

function MoveLos(Z)
	local Unit = Pads.TrackPad3.CFrame - Pads.TrackPad3.CFrame.Position
	Pads.TrackPad3.CFrame = CFrame.new(Pads.TrackPad3.Position.X,Pads.TrackPad3.Position.Y,Z) * Unit
	Models.Referee3.Humanoid:LoadAnimation(Replicated:FindFirstChild('Game').Anims:FindFirstChild('Referee').Chains.Grab):Play()
	Models.Referee3.Humanoid:LoadAnimation(Replicated:FindFirstChild('Game').Anims:FindFirstChild('Referee').Chains.Hold):Play()

	repeat
		Models.Referee3.Humanoid:MoveTo(Vector3.new(Pads.TrackPad3.Position.X,Pads.TrackPad3.Position.Y,Pads.TrackPad3.Position.Z))
		local reached = Models.Referee3.Humanoid.MoveToFinished:Wait()
	until reached
	
	Module.CharFace(Models.Referee3)
end

It still kinda lags in the beginning. Should I just make it client based for when they are moving?

Yes, it is best to make it client based so it replicates better and the server has less strain. You shoulda also probably use CharFace on the client as well. You can eventually set the position on the server so everyone can see it.

I would just copy over all the variables and services and just do it the same way correct? or would I have to change anything. I’ll show you the script if I need too

You shouldn’t need to change much. The only thing you would need to change is probably the variables so they are on the client as well.

What about this line?

Honestly, I’m not sure which line you mean.

The OnServerEvent(char2.00)

Not sure what to do with that line since I have no clue of what it does.

Cause now I have to change these and would I have to change this from a client to a server script? or what could I do?
image

For the buttons btw

Could I see the MoveIncrements function?

function MoveIncrements(Yards)
	if Values.Flipped.Value then
		MoveLos(Pads.TrackPad3.Position.Z - Yards)
	else
		MoveLos(Pads.TrackPad3.Position.Z + Yards)
	end
end
--

Something like this then:

--server
function moveLos(Z)
	Events.Chains.Event:FireAllClients(Z)
end
--client
Events.Chains.Event.OnClientEvent(Z)
	local Unit = Pads.TrackPad3.CFrame - Pads.TrackPad3.CFrame.Position
	Pads.TrackPad3.CFrame = CFrame.new(Pads.TrackPad3.Position.X,Pads.TrackPad3.Position.Y,Z) * Unit
	Models.Referee3.Humanoid:LoadAnimation(Replicated:FindFirstChild('Game').Anims:FindFirstChild('Referee').Chains.Grab):Play()
	Models.Referee3.Humanoid:LoadAnimation(Replicated:FindFirstChild('Game').Anims:FindFirstChild('Referee').Chains.Hold):Play()
	Models.Referee3.Humanoid.WalkToPoint = Vector3.new(Pads.TrackPad3.Position.X,Pads.TrackPad3.Position.Y,Pads.TrackPad3.Position.Z)
	Models.Referee3.Humanoid.MoveToFinished:Connect(function()
		Module.CharFace(Models.Referee3)
	end)
end)
1 Like

Nothing happens when I press LOS to fire the event