Help with RunService

So I was playing around in roblox studio trying to create 1 player 2 character thingy, and I got to shiftlock.

This is how it looks for now:

It works, but the problem is that the second (red) character is faster than the main character.

I’m doing this with run service:

local rad = math.rad
local fO = CFrame.fromOrientation
local cf = CFrame.new
------------------------------------

RS.Heartbeat:Connect(function()
		if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
			local NPC: Model = NPCv.Value
			if not NPC then return end
			local char = plr.Character
			if not char then return end
			local HRP: BasePart = char:FindFirstChild("HumanoidRootPart")
			if not HRP then return end
			local Head2: BasePart = NPC:FindFirstChild("Head")
			if not Head2 then return end
			local X, Y, Z = HRP.Orientation.X, -HRP.Orientation.Y, HRP.Orientation.Z
			NPC:PivotTo(cf(Head2.Position) * fO(rad(X), rad(Y), rad(Z)))
		end
	end)

I think its because its setting the cframe of the NPC (with :PivotTo) a little too fast.
And it also may be due to floating point errors, but idk…

Any help greatly appreciated!

Try using CFrame.new(NPC:GetPivot().Position) for the positional part instead of getting the position. Also I don’t know your specific reasoning for putting the globals as variables, but that doesn’t provide any performance benefits in Luau.

Nope, this didn’t fix it either.

Also, I’ve heard that in quick loops like heartbeat its better to put globals as variables because local variables are indexed faster.

But I might be completely wrong, its just what I read somewhere…

It’s still going to have the performance cost of accessing a normal variable, at least that’s what it says in the Luau documentation

Currently with the way the formula is written, it would result in the NPC teleporting to its own head’s position although rotated to match the inverse of the player character’s root part’s orientation. The reason why I’m mentioning this because it seems the issue might be happening due to another part of code that’s responsible for the NPC’s movement and offset


@wastbo This method originated from standard Lua, which had a performance benefit to storing global functions and methods within a variable before using them within a loop

There is one case where storing a value in a variable before using it within a loop is still ideal for performance, which is to pre-calculate part of a formula before calculating the rest of the result within the loop

I’m using Humanoid:Move() to move the other character, because its how its done in the playermodule.

So I have no access to change how the character is moving because its handled in roblox core scripts (I think.)
I don’t know how else to change rotation of the character without setting its position with it.

It could be that Humanoid:Move is conflicting with the pivot, you might need to use a BindableEvent to send a signal to the NPC’s module to slow it down while shift lock is enabled

Slowing it down would make it worse cause then it still has the choppy movement due to the loop that sets the cframe, and its very hard to find the exact amount of how much to slow it down, cause I want it to be the same speed in sync with my main character.

Is there a way to set characters orientation without having to set the position?

You can try using CFrame.lookAt and use the NPC’s current position as the first argument, then the desired look direction as the second

Yes, but that’s still setting the position.
To do this you would set the pivot of the character.
And setting the pivot in any way will make the character jitter.

You’re already using PivotTo in the code you provided, so you’re already setting the NPC’s position and there doesn’t seem to be any issues with jittering in the clip, only a desync issue with the movement speeding up when shift lock is enabled

Reason why it speeds up is because its setting the position really fast, and with roblox’s physics somehow it makes the character go faster.

It would jitter if the heartbeat event was slower, so it is jittering but very quickly.

The problem is that this is not a speed increase, its setting the position not the speed.
So that makes it difficult to slow down.

One option is to use an AlignOrientation set to two attachment mode, and set one attachment to the NPC root part’s RootAttachment and the other attachment to an attachment that’s located in a spot where you’d like the NPC to look at

1 Like