I made a script for flight and when I fly I have control buttons for phones, but when I die and appear I can fly but the buttons don’t work and I don’t understand what’s the matter
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Check if the device is mobile
if not UserInputService.TouchEnabled then
return -- If not a mobile device, exit the script
end
-- Ensure this frame refers to PlayerGui
local mobilesupport = player:WaitForChild("PlayerGui"):WaitForChild("FlyGui"):WaitForChild("Frame")
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local hrp = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
-- Create animations
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://113158644652516"
local hoverAnim = Instance.new("Animation")
hoverAnim.AnimationId = "rbxassetid://94129276237589"
local lastAnim = nil -- Initially no animation set
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.maxTorque = Vector3.new(1, 1, 1) * 10^6
bodyGyro.P = 10^6
local bodyVel = Instance.new("BodyVelocity")
bodyVel.maxForce = Vector3.new(1, 1, 1) * 10^6
bodyVel.P = 10^4
local isFlying = false
local movement = {forward = 0, backward = 0, right = 0, left = 0}
-- Check if the player has completed 7 quests
local function hasCompletedSevenQuests()
local completedQuests = player:WaitForChild("questNum").Value
return completedQuests >= 10
end
-- Function to update mobile controls visibility
local function updateMobileControls()
mobilesupport.Visible = UserInputService.TouchEnabled and isFlying
end
-- Function to toggle flying and animations
local function setFlying(flying)
isFlying = flying
bodyGyro.Parent = isFlying and hrp or nil
bodyVel.Parent = isFlying and hrp or nil
bodyGyro.CFrame = hrp.CFrame
bodyVel.Velocity = Vector3.new()
-- Update animations
if isFlying then
if lastAnim then
lastAnim:Stop() -- Stop the previous animation if it exists
end
-- Check for humanoid before loading animation
if humanoid then
local success, errorMessage = pcall(function()
lastAnim = humanoid:LoadAnimation(hoverAnim) -- Load hover animation
end)
if not success then
warn("Error loading Hover animation: " .. errorMessage)
else
lastAnim:Play() -- Play hover animation
end
end
else
if lastAnim then
lastAnim:Stop() -- Stop hover animation if it exists
end
lastAnim = nil -- Clear reference to animation
end
updateMobileControls()
end
-- Update flying state
local function onUpdate(dt)
if isFlying then
local cf = camera.CFrame
local direction = cf.rightVector * (movement.right - movement.left) + cf.lookVector * (movement.forward - movement.backward)
if direction:Dot(direction) > 0 then
direction = direction.unit
end
bodyGyro.CFrame = cf
bodyVel.Velocity = direction * humanoid.WalkSpeed * 3
end
end
-- Function to handle button presses
local function buttonAction(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
movement[actionName] = 1
elseif inputState == Enum.UserInputState.End then
movement[actionName] = 0
end
if isFlying then
local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0
-- Switch animation based on movement
local nextAnim = isMoving and humanoid:LoadAnimation(hoverAnim) or humanoid:LoadAnimation(idleAnim)
if nextAnim ~= lastAnim then
if lastAnim then
lastAnim:Stop() -- Stop current animation before switching
end
lastAnim = nextAnim
lastAnim:Play()
end
end
return Enum.ContextActionResult.Pass
end
-- Setup button handlers
local function setupButtonHandlers()
local buttonUp = mobilesupport:WaitForChild("ButtonUp")
local buttonDown = mobilesupport:WaitForChild("ButtonDown")
local buttonLeft = mobilesupport:WaitForChild("ButtonLeft")
local buttonRight = mobilesupport:WaitForChild("ButtonRight")
-- Handle button presses
buttonUp.MouseButton1Down:Connect(function() buttonAction("forward", Enum.UserInputState.Begin) end)
buttonUp.MouseButton1Up:Connect(function() buttonAction("forward", Enum.UserInputState.End) end)
buttonDown.MouseButton1Down:Connect(function() buttonAction("backward", Enum.UserInputState.Begin) end)
buttonDown.MouseButton1Up:Connect(function() buttonAction("backward", Enum.UserInputState.End) end)
buttonLeft.MouseButton1Down:Connect(function() buttonAction("left", Enum.UserInputState.Begin) end)
buttonLeft.MouseButton1Up:Connect(function() buttonAction("left", Enum.UserInputState.End) end)
buttonRight.MouseButton1Down:Connect(function() buttonAction("right", Enum.UserInputState.Begin) end)
buttonRight.MouseButton1Up:Connect(function() buttonAction("right", Enum.UserInputState.End) end)
end
-- Function to handle jump requests (for mobile devices)
local function onJumpRequest()
if not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead then
return
end
-- Check if the player has completed 7 quests before activating flight
if hasCompletedSevenQuests() then
setFlying(not isFlying) -- Toggle flying state on jump button press
else
-- Message for the player that they need to complete 7 quests
print("You need to complete 7 quests to activate flight.")
end
end
-- Handle state change
local function onStateChange(old, new)
if new == Enum.HumanoidStateType.Landed then
isJumping = false
elseif new == Enum.HumanoidStateType.Jumping then
isJumping = true
end
end
-- Connect events
humanoid.StateChanged:Connect(onStateChange)
-- Connect jump button for mobile devices
UserInputService.JumpRequest:Connect(onJumpRequest) -- Use JumpRequest to activate flight
-- Update flying on RenderStepped
RunService.RenderStepped:Connect(onUpdate)
-- Initial setup for mobile controls visibility
mobilesupport.Visible = false -- Start hidden
updateMobileControls() -- Update visibility based on device and state
-- Setup button handlers
setupButtonHandlers()
-- Update character on respawn
player.CharacterAdded:Connect(function(newCharacter)
character = newCharacter
humanoid = character:WaitForChild("Humanoid")
hrp = character:WaitForChild("HumanoidRootPart")
-- Update flying state after respawn
if isFlying then
setFlying(true)
end
-- Update mobile controls visibility on respawn
updateMobileControls()
end)