I’m having issues with my slide script. I want to get my slide to cancel if space is pressed while sliding but I don’t know how to break out of the sliding for loop and reset player speed/velocity while inside of the if statement that detects if a player presses space/jumps.
I have tried
for count = 1, 8 do
wait(0.2)
slide.Velocity *= 0.8
if input.KeyCode == Enum.KeyCode.Space then
break
end
end
and the same thing but “continue” instead of break. I have also tried setting the variable “count” to 8 to stop the for loop but I think it’s not in the same scope.
Here is my whole script if that helps.
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.C then
if gameProcessed then return end
if not canSlide then return end
canSlide = false
local slide = Instance.new("BodyVelocity")
slide.MaxForce = Vector3.new(1, 0, 1) * 30000
slide.Velocity = character.HumanoidRootPart.CFrame.lookVector * SLIDE_SPEED
slide.Parent = character.HumanoidRootPart
-- slide time
for count = 1, 8 do
wait(0.2)
slide.Velocity *= 0.8
if input.KeyCode == Enum.KeyCode.Space then
break
end
end
slide:Destroy()
wait(SLIDE_COOLDOWN)
canSlide = true
end
end)
Also, if it’s not too much trouble, I also would like to know how I can stop the slide when the player reaches a certain speed. I can’t seem to find out how.
You could first create a variable out the outside of the function called something like sliding, that’s on default set to false. Then when you start sliding set the variable to true. So in your for loop, you can replace:
with:
if sliding == false then
Then you can add an elseif to the function, listening for when the user presses the space key. So like this:
elseif input.KeyCode == Enum.KeyCode.Space then
sliding = false
sliding = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.C then
if gameProcessed then return end
if not canSlide then return end
canSlide = false
sliding = true
local slide = Instance.new("BodyVelocity")
slide.MaxForce = Vector3.new(1, 0, 1) * 30000
slide.Velocity = character.HumanoidRootPart.CFrame.lookVector * SLIDE_SPEED
slide.Parent = character.HumanoidRootPart
for count = 1, 8 do
if sliding == false then
break
elseif input.KeyCode == Enum.KeyCode.Space then
sliding = false
end
if sliding == true then
wait(0.2)
slide.Velocity *= 0.8
print(sliding)
end
end
slide:Destroy()
sliding = false
wait(SLIDE_COOLDOWN)
canSlide = true
end
end)
I also tried it without the
if sliding == true then
But it still didn’t work. Is it possible that this could be because space isn’t being constantly checked for because of the wait(0.2)?
local sliding = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.C then
if not canSlide then return end
canSlide = false
sliding = true
local slide = Instance.new("BodyVelocity")
slide.MaxForce = Vector3.new(1, 0, 1) * 30000
slide.Velocity = character.HumanoidRootPart.CFrame.lookVector * SLIDE_SPEED
slide.Parent = character.HumanoidRootPart
-- slide time
for count = 1, 8 do
wait(0.2)
slide.Velocity *= 0.8
if sliding == false then
break
end
end
slide:Destroy()
wait(SLIDE_COOLDOWN)
canSlide = true
elseif input.KeyCode == Enum.KeyCode.Space then
sliding = false
end
end)
The variable input wouldn’t change inside of the function. When the space bar is pressed, the function connected to InputBegan would just be run again, so you need to add another condition for handling when the player presses the space bar like my code above.