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.
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.
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.
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.
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)