How to use weld constraints with degrees

I am attmepting to make my current pet system use welds due to the fact the using heartbet can easily cause lag. Here is the code that is supossed to weld the pet to the player evenly around the player. I used this exact mathematic formula and cframe with heartbeat and it works fine but when I set the position with welds my player gest flug all over the map. I checked and the pet models humanoidpart is unanchored and each part has cancollide off. I also tried using motor6ds and weldconstraints but got the same result.

local function addPet(player, petId)
	
	local function GetPointOnCircle(Degrees, CircleRadius)
		return Vector3.new(math.cos(math.rad(Degrees)) * CircleRadius, 0, math.sin(math.rad(Degrees)) * CircleRadius)
	end
	
	local v = workspace.ClientPets[petId]
	local i = table.find(iTable[player.UserId], petId) 

	local newPosition = (player.Character.HumanoidRootPart.CFrame * CFrame.new(GetPointOnCircle(5, 360/player.SessionStats.PetsEquipped.Value * i))).Position - Vector3.new(0, player.Character.HumanoidRootPart.Size.Y / 2, 0) 

	local Weld = Instance.new("Weld")
	Weld.Parent = player.Character.HumanoidRootPart
	Weld.Part0 = player.Character.HumanoidRootPart
	Weld.Part1 = v.HumanoidPart
	Weld.C0 = player.Character.HumanoidRootPart.CFrame
	Weld.C1 = CFrame.new(newPosition)
end

I think ultimately this is the wrong way to go about this. Welding anything to the player will affect the movment of the player.

I couldnt think of any other alternatives. If i use runservice methods the game lags out the more pets I have.

You don’t need runservice methods. As much as people say to use them, there’s really nothing wrong with using wait. You only really need RunService if you want something to happen every frame reliably.
Computers are powerful, and though an eighth of a second doesn’t seem like a long time, the computer can do almost infinitely more things in wait(.125) than it can in 1/60th or 1/30th of a second like RunService uses. You wouldn’t really notice a pet change course eight times per second anyways, so it’s feasable.

I do need to use runservice if I want it to look smooth. I want it to run every frame or else it will be slightly off espcailly when they have FPS changes. However, that doesnt solve my lag problem either.

It depends on how it is done. If you’re CFraming it, yes. But Roblox has TweenService that runs on C++ rather than Lua, so it will lag a lot less if you use that and set up a new tween 5 or 10 times per second. It will be smooth. You can also use BodyVelocities or BodyPositions, if those haven’t been deprecated since I last looked at them. My point is, for something like this, it is ideal to rely on Roblox’s built-in capabilities instead of calculating the movement yourself.

You were on the right track with welds, but I’ve always found those to be glitchy when welded to a character, and it wouldn’t float around you naturally anyways - i.e. when you rotate it would rotate with you.

None of those would work or help. TweenService every 5 seconds means that the players pets wont move for 5 seconds and have a massive delay. BodyMovers are cool but are finicky and laggy last time I used them for this.

I didn’t say TweenService every 5 seconds. If you want me to convince you, I’ll make a demo place real quick to make sure it works.

sure, right now I dont get what you mean so an example place will work fine.

Alright, here’s 15 pets at random speeds and positions with silky smooth tracking without causing performance issues (according to the script performance monitor) by using TweenService.

I could give you the code if you wished, but it looks like this isn’t a great way to do it. Though it is perfect on the server, the client gets very slight stuttering while walking due to replication. I think the best way to go about this is with BodyMovers but controlled by a LocalScript. You can thus offload the work to the client so that no matter how many people have pets, the server doesn’t do a lot of extra work. You can use SetNetworkOwner to allow a LocalScript to control an unanchored part. The person who owns the pets will get a very smooth experience, and everyone else’s pets will stutter slightly.

The only solution that I can see to prevent any stuttering at all is to have every player do the calculations for everyone’s pets, and render them all locally. That’s a lot of work to do for not much improvement though.

Try making the pets Parts Massless, and parenting the pet to the Humanoid so it doesn’t get stuck being parented to Workspace and causing glitching/flinging issues.

2 Likes