How do I detect if a player is at a certain speed?

Hello, I’m trying to figure out how I can prevent people spamming an ability I made in my script. I also have another script that allows the player to sprint if they press a key. I want to make it so that the player can only use the ability when they are sprinting.

Here is the code:

local UIS = game:GetService(“UserInputService”)
local char = script.Parent
local Humanoid = char:WaitForChild(“Humanoid”)

local slideAnim = Instance.new(“Animation”)
slideAnim.AnimationId = “rbxassetid://6842387765”

local keybind = Enum.KeyCode.C
local canslide = true

UIS.InputBegan:Connect(function(input,gameprocessed)
if gameprocessed then return end
if not canslide then return end

if input.KeyCode == keybind then
	canslide = false
	
	Humanoid.HipHeight = -1
	local playAnim = char.Humanoid:LoadAnimation(slideAnim)
	playAnim.Priority = Enum.AnimationPriority.Action
	playAnim:Play()
			
	local slide = Instance.new("BodyVelocity")
	slide.MaxForce = Vector3.new(1,0,1) * 30000
	slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
	slide.Parent = char.HumanoidRootPart
	
	for count = 1, 8 do
		wait(0.1)
		slide.Velocity *= 0.7
	end
	playAnim:Stop()
	slide:Destroy()
	canslide = true
		Humanoid.HipHeight = 2
end

end)

And by the way, this “ability” I am talking about is a sliding script that allows the player to slide on the ground. Currently the player can slide even when they are not sprinting and when they are sprinting, but I want to make it so that they can only slide when they are sprinting.

Do you know how to solve this?

2 Likes

we can use the character.Humanoid.Running event for this:

local topspeed = 20
character.Humanoid.Running:Connect(function(speed)
	if speed >= topspeed then
		--do whatever you want to happen if a player reaches top speed here
	end
end)

Just put the topspeed var as the maximum speed you want the player to go.

2 Likes

You can get the humanoids WalkSpeed and check if it is higher than a certain number.

local MaxSpeed = 100

if humanoid.WalkSpeed >= MaxSpeed  then
    --Do anything when they reached the highest speed
end

Client modication exists, so this is not a viable method. The client can go set their walkspeed to 500 and it will not be seen by the server, adding it into the startergui makes 0 sense, as the client can delete the script/ modify it.

There is some variation with this, however this is the most accurate method to doing this relatively.

@DevForum_Acc I reccomend you use this.

You can just check on the server

while true do
   if Humanoid.WalkSpeed > 100 then
         plr:Kick()
         return
   end
    wait(3)
end

the walkspeed doesn’t replicate because it’s being set on the client

Why would it be on the client? Exploiters can modify it easily.

oh i meant checking for the walkspeed on the server doesnt work because its being changed on the client

If it is being changed on the client then it wouldn’t replicate on the server. They should handle it using a server script

The way that Roblox works, Players have network ownership over their character, this means that the Player can edit values within their character, whether or not FE is enabled. Exploiters can set the properties on their Humanoid, and those properties won’t replicate to the server. That’s why you should not be checking that, as the value could be different on the server.

As an example, the value the server sees may be 16, but the player is actually moving at 100, because the player used an exploit to change the value.

So try to use RemoteEvent

Local Script:

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

local event = script.Parent:WaitForChild("RemoteEvent")
local slideAnim = script.Parent:WaitForChild("SlideAnim")

local keybind = Enum.KeyCode.C
local canslide = true

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	if not canslide then return end

	if input.KeyCode == keybind then
		canslide = false
		event:FireServer(canslide)
	end
end)

Server Script:

local event = script:WaitForChild("RemoteEvent")
local slideAnim = script:WaitForChild("SlideAnim")

event.OnServerEvent:Connect(function(player, canslide)
	local char = player.Character
	local Humanoid = char:WaitForChild("Humanoid")
	Humanoid.HipHeight = -1
	local playAnim = char.Humanoid:LoadAnimation(slideAnim)
	playAnim.Priority = Enum.AnimationPriority.Action
	playAnim:Play()

	local slide = Instance.new("BodyVelocity")
	slide.MaxForce = Vector3.new(1,0,1) * 30000
	slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
	slide.Parent = char.HumanoidRootPart

	for count = 1, 8 do
		wait(0.1)
		slide.Velocity *= 0.7
	end
	playAnim:Stop()
	slide:Destroy()
	canslide = true
	Humanoid.HipHeight = 2
end)

@kaiyellow2008 Thanks! This helped so much! Players can now only slide while sprinting.