So I have this script here that lets you jump high if you click jumper right as you land
local player = game.Players.LocalPlayer
local function onCharacterAdded(character)
local humanoid = character:WaitForChild('Humanoid')
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed then
print("canbounce")
humanoid.JumpPower = 100
wait(0.4)
humanoid.JumpPower = 50
print("ended")
end
end)
end
player.CharacterAdded:Connect(onCharacterAdded)
and it works perfectly, however the player can simply hold the space bar or spam the jump button so fast that they hit it after they land. How do i prevent the player from just spamming jump? I am a beginner at scripting and this code is from the roblox assistant, so i have no clue how to go about this.
because the function has a wait which yields, you can make a debounce which will block incoming state changed until the last valid state changed is done.
I would add a manual check for if the player is physically holding their spacebar down, then assigning a boolean that turns true when they do. I then pass the boolean through the bounce function as a condition to be met before executing.
The following code shows how to apply it to your situation.
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local player = Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local holdingJump = false
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
holdingJump = true
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
holdingJump = false
end
end)
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed and not holdingJump then
print("canbounce")
humanoid.JumpPower = 100
wait(0.4)
humanoid.JumpPower = 50
print("ended")
end
end)
I don’t feel like spamming would be that much of an issue because they are still pressing the spacebar manually.
This is helpful but spamming is still the primary issue, the game is all about split second precision and difficulty, which disappears when you can just spam space
Then it works in your favor. You already prevented spamming from being a valid tactic because like you said, the way you utilize your jumps require precision and timing. Spamming lacks precision and timing, so they will be inconsistent and make a lot more mistakes than if some one were to not spam the spacebar. So therefore you already rendered spamming the spacebar as a futile attempt.
However, if you persist on wanting to add spam-prevention, how I would go about to fix this issue is add a counter that adds one every time the spacebar is pressed in the same jump. Then when you start the jump boost, you add a condition that the amount of spacebar presses have to be 1 (indicating you have only pressed the spacebar to start the first jump) to actually get the boost. Here’s a quick run-down on how to implement this;
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local player = Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local holdingJump = false
local debounce = false
local jumpAttempts = 0
local function JumpBoost()
humanoid.JumpPower = 100
task.wait(0.4)
humanoid.JumpPower = 50
end
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
holdingJump = true
jumpAttempts += 1
print(jumpAttempts)
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
holdingJump = false
end
end)
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed and debounce == false then
debounce = true
if not holdingJump and jumpAttempts == 1 then
jumpAttempts = 0
print("canbounce")
JumpBoost()
print("ended")
else
jumpAttempts = 0
print("No boost.")
end
task.wait(0.25)
debounce = false
end
end)