CFrame NPC System

Hello all,

In a game I am working on there are going to be NPC characters that are going to be walking around on moving platforms frequently. A problem I have encountered is that with traditional Humanoid NPCs it is quite easy for one of them to fall off. If they jump or happen to trip on something, they will lose all of the momentum that they had from the platform and fall off.

A solution I thought of was having the NPCs be CFramed. I was wondering if this is the best way to go or is there some other solution I could use for traditional Humanoid NPCs. If anyone knows any trick I can use for humanoid NPCs then please let me know. However, if you think I should go for the CFrame option I have a few questions below that I need answered.

  1. How would I handle gravity for the NPCs?
    I was thinking for gravity I could just raycast down but I’ve heard people say raycasting can be expensive so I was wondering if there is some other way I can do it or if raycasting is the best option overall

  2. How should replication be handled?
    I am assuming the client should be cframing each NPC when it needs to walk around. How should the Client and Server communicate? How should the movement of an NPC be replicated to other players?

If anyone has any info on this it would be greatly appreciated. Thanks for the help.

Tbh, I’d just add an invisible “barrier” part to the edges. Basically making them in a box without a roof.
If you make sure you size the part correct, they shouldn’t be able to jump over it.

That wouldn’t solve my problem. Anytime the character loses contact with the floor they will just fly to the back of the ship because roblox doesnt do momentum correctly

To prevent tripping, disable the Ragdoll and FallingDown HumanoidStateTypes.
To prevent jumping, disable the Jumping HumanoidStateType.

Traditional Raycasting is expensive, but I’m pretty sure that WorldRoot:Raycast() is less expensive as it doesn’t use a Ray object.

I don’t have much experience with this, but what I would do is compute the NPC’s position on the server, send the position to the client(s), then have the client lerp, tween, or :MoveTo() the NPC to the aforementioned position, or use whatever NPC pathfinding method you use in your game.

Replication could be handled by tweenservice every 0.1 seconds send a table of NPC data to all the clients. Make all the clients tween there local NPC’s to that position, initial tweens cancel when a new tween is created for the same property and played so don’t worry about the tweens overlapping.

theres many optimizations you could do such as if the NPC is not in the viewport of the character then don’t render it

Wait im confused are you implementing local rendering or replicating client npcs to the server? That’s pretty easy every 0.1 seconds send the position of the npc and tell the server to tween it there

Oh nevermind your doing the client one just make it so that all the clients get a copy of the npc, and then the server tells all the clients to tween there local NPC to the position the client sent it.

Also @overflowed the older version of raycasting is not expensive at all. I use this for my projectile system and I can handle more then 10k raycasts a second.My bullets raycast to the nextpart of there desintation every frame. And I did a stress test and created 400 bullets a second. The bullets max distance is 400. So the bullets travel for one second. And I get 60 fps out of this.

So Im raycasting 60 times a second for each bullet, completelty with no lag. Raycasting is not expensive at all.

1 Like

I was making this statement in comparison to WorldRoot:Raycast().

No they don’t have a much of a difference. The only way you can reduce lag would be reusing raycastparams and it’s not much faster.

Unless if your planning to reuse it there isn’t going to be a difference.