Make Pets always touch the ground

Also this error shows up,


Am I supposed to do that?

Also:

yes, I fixed that in an edit a few minutes ago, also Just add an extra ) after the raycast.

        local ground = workspace:Raycast(pet.PrimaryPart.Position, Vector3.new(0,-100,0))
        if ground and ground.Instance then
            local petC= pet.PrimaryPart.CFrame
            pet:SetPrimaryPartCFrame(CFrame.new(Vector3.new(petC.Position.X, ground.Instance.Position.Y + (ground.Instance.Size.Y/2) + (pet.PrimaryPart.Size.Y/2), petC.Position.Z)))
        end


So how do I fix that?

that confuses me, is there some sort of invisible wall in the map? It should be directly above the ground. Then again, I am checking beneath the pet, so it might be getting some other wall or object from being in a different position. Might have to use the player’s character as the origin for the Raycast instead.

local ground = workspace:Raycast(character.PrimaryPart.CFrame.Position, Vector3.new(0,-100,0))

No you don’t need any raycast, just add a align position to the pet and 2nd attachment to the player leg

Isn’t there a way to ignore certain parts? Also the pets don’t turn when I move to lets say the right, etc., how do I fix that?

Can you give an example on what you mean?

I’d have to add an extra thing to the CFrame when setting it for the rotation. As for the ignoring certain parts, you can use RayParams.FilterDescendantsInstances and if there is a folder for those, make it equal to that folder. Fix to the Rotation should be like this:

        local ground = workspace:Raycast(character.PrimaryPart.CFrame.Position, Vector3.new(0,-100,0))
        if ground and ground.Instance then
            local petC= pet.PrimaryPart.CFrame
            pet:SetPrimaryPartCFrame(CFrame.new(Vector3.new(petC.Position.X, ground.Instance.Position.Y + (ground.Instance.Size.Y/2) + (pet.PrimaryPart.Size.Y/2), petC.Position.Z), Vector3.new(petC.Rotation)))
        end

That can be inaccurate if the ground has slopes or ledges.

Also, this just seems very laggy and doesn’t look well.

Sometimes it looks like it will glitch up sometimes

Probably because I didn’t use lerp, which I honestly should have done in the beginning like you had.

        local ground = workspace:Raycast(character.PrimaryPart.CFrame.Position, Vector3.new(0,-100,0))
        if ground and ground.Instance then
            local petC= pet.PrimaryPart.CFrame
            pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(Vector3.new(petC.Position.X, ground.Instance.Position.Y + (ground.Instance.Size.Y/2) + (pet.PrimaryPart.Size.Y/2), petC.Position.Z), Vector3.new(petC.Rotation)))
        end

If you ever wondered pet sim x, you would notice the pets will stay on the back side of player, and will always remain at the back, but they just have the walking animation when they are moving

SImilarly, you don’t do ray cast each time the player moves, this causes a bit of lag, you need to raycast whenever the altitude of player changes, like if the player falls down a building

Then you can fire the raycast with the function humanoid.StateChanged, 5 states

  • None
  • Walking
  • Running
  • Freefall
  • Jumping

Whenever the player gets the freefall state you can run the ray cast to get the ground of the pet,if any

This will improve game performance by x3 compared to infinite ray casting

You can use a weld constraiit or a align position to keep it moving along with player
Align position also called body position, is used if you need your pet to animate, rather than just look unrealistic teleport

Raycasting takes alot of space and will lag on devices, thats not recommended in things like pets which constantly moves

Maybe, but you can also use AlignOrientation to fix that

Are there any videos on how to do what you said, or any DevForum post? I’m curious.

Idk but I found a documentation
https://developer.roblox.com/en-us/api-reference/class/AlignPosition

I meant about this
“No you don’t need any raycast, just add a align position to the pet and 2nd attachment to the player leg”

Raycasting is a pretty simple operation. There’s no reason this should cause lag on its own, even if you’re doing hundreds (possibly thousands) of rays a second. Any lag caused is most likely caused by something else you’re doing.

Yes check the link, I sent, I’ve made several things using that align position

Yes, but if you have over a 10 players, then the server would have to run ray cast several times every second to animate the pets, this will sometimes break the script or cause warnings, I tried to run a script 1k times a second, a printing script, a few seconds later, the server crashed and everyone in the server had 5k-10k and it was totally messed up, but his game already have some stuff, if we add ray cast than it lags up alot compared to align position, thats x10 laggier and x3 less smoother

As long as you have reasonable delays in your loop, there won’t ever be an issue. Even if there were a hundred players, this simple operation wouldn’t matter.

However, I would highly recommend doing this client-side in this situation. Roblox’s server’s are known to be rather weak. Even though raycasting wouldn’t be an issue, it’s always better to do anything visual on the client. The server should only have to deal with secure data, cross-referencing and validation, and client-server communication.

Also on this note, raycasting and align position do two very different things.