Hey everyone, I want the players in my game to be punished when they’re moving and shooting certain weapons. The way I’ve handled it so far was to use the Humanoid.Running event to set a value which the gun uses to determine how accurate the shot is.
It’s only until now I realized that if you were to stop suddenly and shoot, say you were peaking someone, the shot will still be as inaccurate as if you were running. The speed value stays for about a second before correcting itself.
I’ve tried using RunService to set the runSpeed value to the HumanoidRootPart.Velocity.Magnitude but it behaved the same.
--StarterCharacterScripts
local humanoid = script.Parent:WaitForChild("Humanoid")
local runSpeed = Instance.new("IntValue")
runSpeed.Parent = char
runSpeed.Name = "RunSpeed"
humanoid.Running:Connect(function(speed)
runSpeed.Value = speed
end)
Pair this with Humanoid.MoveDirection.Magnitude since Running isn’t fast enough, and if it’s 0 then we know that the character is completely still.
local humanoid = script.Parent:WaitForChild("Humanoid")
local runSpeed = Instance.new("IntValue")
runSpeed.Parent = script.Parent
runSpeed.Name = "RunSpeed"
local stillspeed = 0 -- speed when someone is completely still
local movespeed -- initialize a variable for speed when something is moving
humanoid.Running:Connect(function(speed)
movespeed = speed -- set the speed we got from this to the movespeed variable, but don't use it yet
end)
game:GetService("RunService").Heartbeat:Connect(function() -- frequent checking
if humanoid.MoveDirection.Magnitude <= stillspeed then -- if they stopped moving,
runSpeed.Value = stillspeed -- set this to 0 immediately.
else
runSpeed.Value = movespeed -- we can confirm the person is still moving, use the variable from earlier
end
end)