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

How do I stop a "touch" function after being touched?

Asked by
oh_blah 10
2 years ago

So I have this fly script that I just added touch function to..When using the pc, the triple space bar command for flight works perfectly, and when i want to cancel flight, the triple space bar command continues to work perfectly. However, when using a mobile device with touch screen, to toggle flight with triple touch, the flight works fine but when canceling flight after triple touch function, for some reason when i am back on the ground, my character's movements is still influenced as if it were flying. In other words, the flight was not fully cancelled. How do we stop the "touch" function after being touched? Also is it possible to toggle the flight touching the jump button three times rather than any part of the screen?Please help me, i am still fairly new to scripting. Below is my Flight Script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://6789722102"
local PlayAnim = Humanoid:LoadAnimation(Anim)
local HumaoidRP = Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()


local TapTime = .2
local Tapped = false
local Toggle = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.Space or Input.UserInputType == Enum.UserInputType.Touch then
        if not Tapped then
            Tapped = true
            wait(TapTime)
            Tapped = false
        else
            if Toggle == false then
                local Jump = Instance.new("BodyVelocity",HumaoidRP)
                Jump.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
                Jump.Velocity = Vector3.new(0,50,0)
                game.Debris:AddItem(Jump,.5)
                wait(.5)
                HumaoidRP.Anchored = true
                Toggle = true
            elseif Toggle == true then
                Toggle = false
                HumaoidRP.Anchored = false
                local Children = HumaoidRP:GetChildren()
                for i, child in pairs(Children) do
                    if child:IsA("BodyVelocity") then
                        child:Destroy()
                    end
                end
            end
        end
    end
end)
UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.W or Input.UserInputType == Enum.UserInputType.Touch then
        if Toggle == false then return end
        PlayAnim:Play()
        HumaoidRP.Anchored = false
        if HumaoidRP:FindFirstChildOfClass("BodyVelocity") then
            HumaoidRP:FindFirstChildOfClass("BodyVelocity"):Destroy()
        end
        local Forward = Instance.new("BodyVelocity",HumaoidRP)
        Forward.Name = "ForwardMovement"
        Forward.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
        local Gyro = Instance.new("BodyGyro",HumaoidRP)
        Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
        Gyro.D = 100
        Gyro.P = 10000
        while Toggle == true do
            Forward.Velocity = Mouse.Hit.lookVector*150
            Gyro.CFrame = Mouse.Hit
            wait()
        end
    end
end)
UIS.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.W or Input.UserInputType == Enum.UserInputType.Touch then
        if Toggle == false then return end
        if HumaoidRP:FindFirstChild("ForwardMovement") then
            HumaoidRP.ForwardMovement:Destroy()
            HumaoidRP.Anchored = true
            PlayAnim:Stop()
            if HumaoidRP:FindFirstChildOfClass("BodyGyro") then
                HumaoidRP:FindFirstChildOfClass("BodyGyro"):Destroy()
            end
        end
    end
end)
UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.S or Input.UserInputType == Enum.UserInputType.Touch then
        if Toggle == false then return end
        HumaoidRP.Anchored = false
        if HumaoidRP:FindFirstChildOfClass("BodyVelocity") then
            HumaoidRP:FindFirstChildOfClass("BodyVelocity"):Destroy()
        end
        local Back = Instance.new("BodyVelocity",HumaoidRP)
        Back.Name = "BackMovement"
        Back.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
        local Gyro = Instance.new("BodyGyro",HumaoidRP)
        Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
        Gyro.D = 100
        Gyro.P = 10000
        while Toggle == true do
            Back.Velocity = Mouse.Hit.lookVector*-100
            Gyro.CFrame = Mouse.Hit
            wait()
        end
    end
end)
UIS.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.S or Input.UserInputType == Enum.UserInputType.Touch then
        if Toggle == false then return end
        if HumaoidRP:FindFirstChild("BackMovement") then
            HumaoidRP.BackMovement:Destroy()
            HumaoidRP.Anchored = true
            if HumaoidRP:FindFirstChildOfClass("BodyGyro") then
                HumaoidRP:FindFirstChildOfClass("BodyGyro"):Destroy()
            end
        end
    end
end)

`

1 answer

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
2 years ago
Edited 2 years ago

with using debounce (aka. cooldown)

heres how you do it:

local UIS = game:GetService("UserInputService")
cooldown = false

UIS.InputBegan:Connect(function(Input,Event) -- event is game processed event (eg. chatting, leaderboards etc.)
    if Input.UserInputType == Enum.UserInputType.Touch and not Event or Input.KeyCode == Enum.KeyCode.W and not Event then
        if cooldown == false then
            cooldown = true
            -- your stuff here
            wait(1) -- replace 1 with the cooldown time, in seconds.
            cooldown = false
        end
    end
end)

here are some other links that may help:

UserInputService

Booleans (what is used for the cooldown)

0
Thank you so much! I used your script as a reference but it didnt solve my issue. But your links were informative. i came across contextActionService and decided a separate button should be created for mobile devices where that button will emulate the space bar. This will rid any issues of players using tools and accidentally toggling flight. Can you help me go about scripting this if you can plz? oh_blah 10 — 2y
Ad

Answer this question