hello, i made a double jump script, but I only want it to work when the player is touching a part, so how would I detect it?
can i see your script so far so i can make my code fit in nicely?
local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local MAX_JUMPS = 2
local TIME_BETWEEN_JUMPS = 0.2
local numJumps = 0
local canJumpAgain = false
local function onStateChanged(oldState, newState)
if Enum.HumanoidStateType.Landed == newState then
numJumps = 0
canJumpAgain = false
elseif Enum.HumanoidStateType.Freefall == newState then
wait(TIME_BETWEEN_JUMPS)
canJumpAgain = true
elseif Enum.HumanoidStateType.Jumping == newState then
canJumpAgain = false
numJumps += 1
end
end
local function onJumpRequest()
if canJumpAgain and numJumps < MAX_JUMPS then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
humanoid.StateChanged:Connect(onStateChanged)
UserInputService.JumpRequest:Connect(onJumpRequest)
1 Like
Here
local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local MAX_JUMPS = 2
local TIME_BETWEEN_JUMPS = 0.2
local numJumps = 0
local canJumpAgain = false
local touchingPart = false
local part = workspace.Part -- change to the part you want the player to be touching
part.Touched:Connect(function(hit)
if hit:IsDescendantOf(character) then
touchingPart = true
end
end)
part.TouchEnded:Connect(function(hit)
if hit:IsDescendantOf(character) then
touchingPart = false
end
end)
local function onStateChanged(oldState, newState)
if touchingPart then
if Enum.HumanoidStateType.Landed == newState then
numJumps = 0
canJumpAgain = false
elseif Enum.HumanoidStateType.Freefall == newState then
wait(TIME_BETWEEN_JUMPS)
canJumpAgain = true
elseif Enum.HumanoidStateType.Jumping == newState then
canJumpAgain = false
numJumps += 1
end
end
end
local function onJumpRequest()
if canJumpAgain and numJumps < MAX_JUMPS then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
humanoid.StateChanged:Connect(onStateChanged)
UserInputService.JumpRequest:Connect(onJumpRequest)
how would i make it so it works for every part in the workspace?
1 Like
try this:
local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local MAX_JUMPS = 2
local TIME_BETWEEN_JUMPS = 0.2
local numJumps = 0
local canJumpAgain = false
local touchingPart = false
game:GetService("RunService").RenderStepped:Connect(function()
for i, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit:IsDescendantOf(character) then
touchingPart = true
end
end)
v.TouchEnded:Connect(function(hit)
if hit:IsDescendantOf(character) then
touchingPart = false
end
end)
end
end
end)
local function onStateChanged(oldState, newState)
if touchingPart then
if Enum.HumanoidStateType.Landed == newState then
numJumps = 0
canJumpAgain = false
elseif Enum.HumanoidStateType.Freefall == newState then
wait(TIME_BETWEEN_JUMPS)
canJumpAgain = true
elseif Enum.HumanoidStateType.Jumping == newState then
canJumpAgain = false
numJumps += 1
end
end
end
local function onJumpRequest()
if canJumpAgain and numJumps < MAX_JUMPS then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
humanoid.StateChanged:Connect(onStateChanged)
UserInputService.JumpRequest:Connect(onJumpRequest)
but i dont think it will let you double jump because double jumping is supposed to be a jump when the player is airborne and you cant really be touching a part if your in the air