RunService:UnbindFromRenderStep removed different functions with same reference name Runn 32 times.
This script makes you run after walking for 2 seconds. The script is In StarterCharacterScripts. How do I fix the error
local Character = script.Parent
local Run = game:GetService("RunService")
local Humanoid:Humanoid? = Character:WaitForChild("Humanoid")
function Runnn()
Character.Humanoid.WalkSpeed = 25
end
Humanoid.Changed:Connect(function(Property)
if Property == "MoveDirection" then
if Humanoid.MoveDirection.Magnitude >= 1 then
task.delay(2,function()
if Humanoid.MoveDirection.Magnitude >= 1 then
Run:BindToRenderStep("Runn", Enum.RenderPriority.Character.Value, Runnn)
end
end)
else
Run:UnbindFromRenderStep("Runn")
Character.Humanoid.WalkSpeed = 16
end
end
end)
Different functions are bound under the same name Runn before the existing ones are disconnected. And MoveDirection can change multiple times despite magnitude always being 1.
I would not use render step if I were you. In the following script, each time move direction magnitude on X and Z axis is detected to be above 0, script initiates StartedRunningTime and waits for 2 seconds. If no function has modified the variable by the time two second yield is over, it checks whether magnitude is still non-zero and applies new walk speed. Any magnitude change updates the StarterRunningTime and prevents the if-statement from continuing.
Keep in mind that Connect() creates a new ‘lua’ thread.
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local MoveDirectionMag: Vector3
local StartedRunningTime: number
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
MoveDirectionMag = (Humanoid.MoveDirection * Vector3.new(1,0,1)).Magnitude
if MoveDirectionMag ~= 0 then
StartedRunningTime = time()
task.wait(2)
if time() - StartedRunningTime >= 2 and (Humanoid.MoveDirection * Vector3.new(1,0,1)).Magnitude ~= 0 then
Humanoid.WalkSpeed = 25
end
else
StartedRunningTime = 0
Humanoid.WalkSpeed = 16
end
end)
That… that’s something I thought would make a difference, but now that you mention it, I realize it changes nothing. When I sent that I forgot MoveDirection is always 0 on Y-axis. So I wanted to prevent the script from thinking a player is running if they were actually falling and their move direction was 0,-1,0 or so. But that doesn’t happen, so you can safely remove MoveDirectionMag.
If Y-axis was actually included too, multiplying Y-axis with zero would nullify it, e.g.
0,-1,-1 → 0,0,-1
Thanks for bringing that up.
Changed version
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local StartedRunningTime: number
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.Magnitude ~= 0 then
StartedRunningTime = time()
task.wait(2)
if time() - StartedRunningTime >= 2 and Humanoid.MoveDirection.Magnitude ~= 0 then
Humanoid.WalkSpeed = 25
end
else
StartedRunningTime = 0
Humanoid.WalkSpeed = 16
end
end)