Hi, I am creating a custom movement script for my game. For some reason when I play in-game, half the time my speed is twice as fast as it should be. In studio, this works perfectly 100% of the time. At the moment I have no attempted solution because I don’t know why this is occurring. This bug has been a major road block in the game’s development, so I am somewhat desperate to fix it.
Issue Visual
Regular:
Double speed:
Relevant Context for Movement Script
Movement is tied to RunService:BindRenderStepped(). The priority is under the Camera’s render priority. V3_dir contains the status of keys pressed. If the X property is 1, then D is pressed, and if it’s -1, then A is pressed. If the Z property is 1, then S is pressed, and if its -1, then W is pressed. If the X property is 0, neither D nor A is pressed. If the Z property is 0, neither W nor S is pressed. V3_dir always has a magnitude of 1.
Discoveries
- I attempted to see if the function terminates before the next frame is rendered, and it does regardless if I’m going at the normal speed or the doubled speed. This means the function isn’t running on two separate threads, which I believe is good.
- The change in position between sequential HRP positions (in the code it is CF_new.p - i) shows a doubled value when the speed is doubled, and a normal value when the speed is normal. This is expected.
- The magnitude of the change in CFrame (in the code is is CF_deltaCF) for the HRP is at it’s expected value when I’m going fast or at the regular speed, which is unexpected. With a doubled speed, one would expect the change in CFrame to also be doubled.
Code:
game:GetService("RunService"):BindToRenderStep("Movement",Enum.RenderPriority.Camera.Value - 1,function(Num_dt)
...
print("Begin: "..tostring(n).." - "..tostring(tick()))
local CF_curHrp = Inst_player.Character.HumanoidRootPart.CFrame
local V3_camLv = ControlManager.Camera.Camera.CFrame.lookVector
local CF_rot = CFrame.new(Vector3.new(),Vector3.new(V3_camLv.X,0,V3_camLv.Z))
local CF_hrpPos = CFrame.new(CF_curHrp.p)
local CF_deltaCF = CFrame.new(V3_dir*ControlManager.SpeedMultiplier*Num_dt)
local CF_new = CF_hrpPos*CF_rot*CF_deltaCF
print((CF_new.p - i).magnitude,"\t \t \t",CF_rot.p.magnitude,"\t \t \t",CF_deltaCF.p.magnitude)
i = CF_new.p
Inst_player.Character:SetPrimaryPartCFrame(CF_new)
print("End: "..tostring(n).." - "..tostring(tick()))
n = n + 1
...
end)
I apologize for the long post. If anyone has any idea as to what may cause this, I would appreciate the insight. Thank you.