Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my script activate when I press Space multiple times instead of one?

Asked by 4 years ago

Hey. I want to make a script that makes the player fall slower when they're at least 300 studs above the ground and have pressed Space. I wrote the script but for some reason it doesn't activate unless I press Space multiple times after I've began falling. And it takes a long time to activate. Any help?

Script:

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local char = player.Character

local Hum = char:WaitForChild("Humanoid")

local Look = script.Parent:WaitForChild("HumanoidRootPart")

local GlideThing = script:WaitForChild("GlideThing")

local Speed = Hum.WalkSpeed

local HumanoidState = nil

local canGlide = false

local gliding = false

local KeyPressed = false

Hum:ChangeState(Enum.HumanoidStateType.Jumping)

Hum.StateChanged:Connect(function()
    local State = Hum:GetState()
    if State == Enum.HumanoidStateType.Freefall then
        HumanoidState = "FreeFall"
    else
        HumanoidState = nil
    end
end)

function Check()
    if HumanoidState == "FreeFall" then
        local ray = Ray.new(Look.CFrame.p, Look.CFrame.UpVector * -300)
        local part, position = workspace:FindPartOnRay(ray, char, false, true)
        if part then
            canGlide = true
        else
            canGlide = false
        end
    else
        canGlide = false
    end
end

function Check2()
    local State = Hum:GetState()
    if State == Enum.HumanoidStateType.Landed then
        canGlide = false
    end
end

Hum.StateChanged:Connect(function(OldState, NewState)
    if NewState == Enum.HumanoidStateType.Running then
        canGlide = false
        GlideThing.Parent = script
    end
end)

Hum.StateChanged:Connect(function(OldState, NewState)
    if NewState == Enum.HumanoidStateType.Landed then
        canGlide = false
        GlideThing.Parent = script
    end
end)

function Glide()
    Check()
    if canGlide == true then
        wait(.3)
        Check2()
        if canGlide == true and KeyPressed == true then
            if gliding == false then
                gliding = true
                Speed = Hum.WalkSpeed
                Hum.WalkSpeed = 50
                GlideThing.Parent = char:WaitForChild("Head")
                KeyPressed = false
            end
        end
    else
        if gliding == true then
            gliding = false
            Hum.WalkSpeed = Speed
            KeyPressed = false
            if GlideThing.Parent ~= script then
                GlideThing.Parent = script
            end
        end
    end
end

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Space then
        KeyPressed = true
        Glide()
    end
end)
0
Sorry, but this code is a complete mess and a lot of it is never going to run. I'd try to help but I dont have time to go through 100 lines of code myself, so please remove the parts that are unused and more people will be willing to help IceAndNull 142 — 4y

Answer this question