Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I made this Fly script and im trying to make it in a gui for mobile why wont it work?? Help!

the fuction wont seem to start when i click my gui... code:

local rs = game:GetService("RunService")

local myPlayer = game.Players.LocalPlayer
local myChar = myPlayer.Character
local myHRP = myChar:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera

local flying = false
local speed = 0.5


local bp = Instance.new("BodyPosition", myHRP)
bp.MaxForce = Vector3.new()
bp.D = 10
bp.P = 10000

local bg = Instance.new("BodyGyro", myHRP)
bg.MaxTorque = Vector3.new()
bg.D = 10

function fly()
	flying = true
	bp.MaxForce = Vector3.new(400000,400000,400000)
	bg.MaxTorque = Vector3.new(400000,400000,400000)
	while flying do
		rs.RenderStepped:wait()
		bp.Position = myHRP.Position +((myHRP.Position - camera.CFrame.p).unit * speed)
		bg.CFrame = CFrame.new(camera.CFrame.p, myHRP.Position)
	end
end

function endFlying()
	bp.MaxForce = Vector3.new()
	bg.MaxTorque = Vector3.new()
	flying = false
end

if button.mouseButton1Down

 then
		if not flying then
			fly()
		else
			endFlying()
		end
	end

1 answer

✓ Accepted answer

Hey!

MouseButton1Down is an event, not a property. Therefore you would need to connect it to a function and said function will run whenever the button is clicked. Code is shown below...

button.MouseButton1Down:Connect (function() -- This function() end is called an anonymous function. All it means is that we can't refer back to it later. (which we don't need to)
	if not flying then
		fly()
	else
		endFlying()
	end
end)

If you have any more questions, feel free to ask.

If this helped, please make sure to accept the answer.

1 comment

0

Tysm!!

This discussion is preserved as it was. New answers, comments, and votes are closed.