Tower Defense: Enemy glitching on the ground

So I’m working on a Tower Defense Game and I’ve started with the Enemy System using Client-side Rendering. But I’ve encountered a problem while working on it. I’ve seen that enemies are glitching to the ground as shown in the video here: https://gyazo.com/a084146863c446b71237b9aff03707a3

I’ve tried changing the offset of the Enemy but seems like it doesn’t change anything. I’ve also tried switching from TweenService to Lerping to move the enemies but to no avail.

Here’s the server-side code for handling the enemies.

function EnemyService:Heartbeat(dt)
	for id, enemyInfo in pairs(self.EnemiesInGame) do
		local nodes = game.Workspace.Map.Nodes

		local currentNode = nodes[enemyInfo.Node]
		local nextNode = nodes[enemyInfo.Node + 1]

		if nextNode then
			local distanceToNextNode = (enemyInfo.CFrame.Position - nextNode.Position).Magnitude
			local moveDistance = enemyInfo.Speed * dt * 10

			if moveDistance >= distanceToNextNode then
				enemyInfo.Node = enemyInfo.Node + 1 
				enemyInfo.CFrame = CFrame.new(nextNode.Position, nodes[enemyInfo.Node + 1].Position)
			else
				local direction = (nextNode.Position - currentNode.Position).Unit 
				local newPosition = enemyInfo.CFrame.Position + direction * moveDistance

				enemyInfo.CFrame = CFrame.new(newPosition, nextNode.Position)
			end

			self.ClientEnemies[id].CFrame = enemyInfo.CFrame
		end
	end

	Networking.Remotes.sync_clients:FireAllClients(self.ClientEnemies)
end

Here’s the code to the client-side for handling enemies.

function EnemyServiceClient:HandleEnemyMovement(enemiesInGame)
	for id, enemyInfo in pairs(enemiesInGame) do
		local enemyModel = game.Workspace._Enemies:FindFirstChild(tostring(id))

		if enemyModel and enemyModel.PrimaryPart then			
			local tween = TweenService:Create(
				enemyModel.PrimaryPart,
				TweenInfo.new(0.1, Enum.EasingStyle.Linear),
				{CFrame = enemyInfo.CFrame}
			)
			tween:Play()
		end
	end
end
1 Like

Hello!

From the gif provided could it be that the unit is trying to look at a point which is below the ground? You should maybe try and sanitize the Y position so that both the current and the next node/position is on the same Y level if anything.

In the clip the NPC seems to constantly teleport underground- so maybe its an issue with newPosition’s Y position?

2 Likes

I’m not a huge scripting pro, but i believe that if you tween something on the client, it won’t move at all on the server. So maybe your issue has something to do with that.

1 Like

That’s the whole point of client-side rendering. Removing any parts on the server and rendering it all on the client.

1 Like

Is the HRP anchored???

araweomaownfwa

1 Like

Anchoring it just breaks it.
image

1 Like

Why not use :MoveTo tho?
And maybe change the HipHeight of the Humanoid?

1 Like

Sorry for the late reply. The reason for not using :MoveTo is that I’ve heard it can be pretty performance-heavy when handling 200+ enemies, and it can create some lag for users. Secondly, I’ve switched from using Humanoids in each enemy.

1 Like

Most simple way would be to just give the Movement Green Parts things a little altitude to fit the enemy.

1 Like

I just set an offset based on each enemy since they vary in size.

1 Like

I previously responded, my solution was wrong but the reason is still correct to my knowledge.

@xKaveyu , are you spawning the enemies in on the server or the client?

1 Like

I’m spawning enemies on the client.

1 Like

Okay. Are you using humanoids?

1 Like

In the video I provided, I was using Humanoids. But I’ve switched from using them and the same result.

1 Like

I see that you have set offsets based on enemy size and whatnot so I doubt that’s the issue. Can you try setting all of your model’s parts to massless? It might be that one of your parts is accidently weighing down the others while tweening.

1 Like

Alrighty, I’ll try to do that and I’ll let you know.

1 Like

The issue you’re encountering with enemies glitching to the ground in your Roblox Tower Defense Game is likely related to the way you are handling enemy movement and synchronization between the server and the client. Here are some steps to help you diagnose and potentially resolve this problem:

  1. Check the Enemy Models in the Workspace: Ensure that your enemy models in the Workspace have their PrimaryPart set correctly. The PrimaryPart should be the part of the enemy model that represents its position. It’s used as a reference point for positioning and tweening. Double-check that all enemy models have a PrimaryPart set.
  2. Adjust Tweening Parameters: It looks like you’re using TweenService to handle enemy movement. You might need to experiment with the TweenInfo and timing values to ensure smooth movement. For example, try adjusting the TweenInfo duration (0.1 seconds) and EasingStyle to see if it makes a difference. Slower, smoother movement may help reduce glitches.
  3. Synchronize Server and Client: Make sure that the server and client are in sync regarding enemy positions and movement. It’s crucial that both sides have the same data. Check that there are no discrepancies between the server’s enemyInfo.CFrame and the client’s enemyModel.PrimaryPart.CFrame. Any desynchronization can lead to glitches.
  4. Physics and Collision Issues: Consider checking if there are any physics or collision-related problems. It’s possible that the enemy models are colliding with something in the environment, causing them to glitch. Make sure the enemy models have CanCollide set to false if they don’t need collision with other parts.
  5. Debugging: Add debugging statements to your code to help pinpoint the issue. Print out the values of enemyInfo.CFrame, enemyModel.PrimaryPart.CFrame, and other relevant variables at various points in your code to see if there are any unexpected changes or discrepancies.
  6. Consistency in Data Transmission: Ensure that data transmission between the server and client is consistent and reliable. If you’re using RemoteEvents or RemoteFunctions, make sure they are working correctly and that the data being sent is accurate.
  7. Alternative Approaches: If the issue persists, consider trying alternative approaches to enemy movement, such as using a different tweening library or using physics-based movement with BodyVelocity or BodyPosition. Sometimes, changing the way you handle movement can help resolve unexpected glitches.
  8. Update Roblox Studio and Libraries: Ensure that you are using an up-to-date version of Roblox Studio and the necessary libraries. Sometimes, updating to the latest version can resolve unexpected issues.

Looks like you used ChatGPT to create that lol.

Can we all stop using ChatGPT for DevForum responses. It defeats the whole point of this website, which is to ASK FOR HELP FROM OTHER PEOPLE.
This is getting out of hand, and Roblox has done barely anything about this.

1 Like

well atleast i tried to help it isn’t about the point it is about helping people so please i just try to help and it actually is fair and also smart and tell me what did you do to help yeah exactly nothing