Flying script help

Im making a script, I want the player to gain 20xp every 3 seconds. I dont know how, maybe make text pop up too and make it compatable for a 2x gamepass! I have got the flight script.

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse() 
local character = player.Character or player.CharacterAdded:Wait() 
local BV = Instance.new("BodyVelocity",character:WaitForChild("HumanoidRootPart")) 

local speed = 3

local Bind = false
local UIS = game:GetService("UserInputService")
BV.MaxForce = Vector3.new(0,0,0)
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not Bind then
		Bind = true
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		while true do 
			game.Players.LocalPlayer.Backpack.XP.Value = 20  ---- RIGHT HERE I NEED IT ALL, STUCK ON THIS!
			wait(3)
		end
		
	elseif input.KeyCode == Enum.KeyCode.F and Bind then
		Bind = false
		BV.MaxForce = Vector3.new()
	end
end)
mouse.Move:Connect(function() 
	BV.Velocity = CFrame.new(character.HumanoidRootPart.Position,mouse.Hit.Position).LookVector * speed

Is this in a localscript? UserInputService only works in local scripts. If you want something to replicate to the server, you’ll have to separate it into a script and bridge the two with RemoteEvents.

Yes, its in a local script on StarterPlayerScripts!

Currently every time they press [f], they get another connection. Therefore if they press f 100 times they will get 2000 exp every 3 seconds. What I would do is this have the loop at the bottom that only runs when bind == true. Also you should have a remote event to fire the server.

local speed = 3

local Bind = false
local UIS = game:GetService("UserInputService")
BV.MaxForce = Vector3.new(0,0,0)
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not Bind then
		Bind = true
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		
	elseif input.KeyCode == Enum.KeyCode.F and Bind then
		Bind = false
		BV.MaxForce = Vector3.new()
	end
end)

while Bind == true do --only runs while Bind is true
        wait(3)
        game.Players.LocalPlayer.Backpack.Xp.Value += 20
end

Well I tried, its not working but it flys not earns XP.

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse() 
local character = player.Character or player.CharacterAdded:Wait() 
local BV = Instance.new("BodyVelocity",character:WaitForChild("HumanoidRootPart")) 

local speed = 3

local Bind = false
local UIS = game:GetService("UserInputService")
BV.MaxForce = Vector3.new(0,0,0)
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not Bind then
		Bind = true
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

	elseif input.KeyCode == Enum.KeyCode.F and Bind then
		Bind = false
		BV.MaxForce = Vector3.new()
	end
end)

while Bind == true do --only runs while Bind is true
	wait(3)
	game.Players.LocalPlayer.leaderstats.XP.Value += 20
	end

mouse.Move:Connect(function() 
	BV.Velocity = CFrame.new(character.HumanoidRootPart.Position,mouse.Hit.Position).LookVector * speed
	end)

Any errors? Also move the while loop to the bottom of the script, any code below it won’t run.

Wheres the loop and no errors, are you on studio if you can see I might be wrong…

Maybe try this?

local speed = 3

local Bind = false
local UIS = game:GetService("UserInputService")
BV.MaxForce = Vector3.new(0,0,0)
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not Bind then
		Bind = true
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

	elseif input.KeyCode == Enum.KeyCode.F and Bind then
		Bind = false
		BV.MaxForce = Vector3.new()
	end
end)


mouse.Move:Connect(function() 
	BV.Velocity = CFrame.new(character.HumanoidRootPart.Position,mouse.Hit.Position).LookVector * speed
	end)

while true do 
       if Bind == true then
	        wait(3)
         	game.Players.LocalPlayer.leaderstats.XP.Value += 20
       end
end

22:58:44.094 Script timeout: exhausted allowed execution time - Client - LocalScript:29

while true do 
	if Bind == true then
		wait(3)
		game.Players.LocalPlayer.leaderstats.XP.Value += 20
	end
end

The conditional exists for a reason, writing your while loops like this only makes your code harder to read.

This doesn’t work because of the while true do without any yielding. Bind is never set to true, so the code never yields and causes a crash (the error you see).


while Bind == true do --only runs while Bind is true
    wait(3)
    game.Players.LocalPlayer.leaderstats.XP.Value += 20
end

This doesn’t work because it evaluates if Bind is true, then if it isn’t, moves on. Since bind isn’t true when the loop is run, nothing happens and the loop ends without doing anything.


Just use the original code, and change while true do to while Bind do. Everytime they press F, the Bind variable is toggled, ending the loop.


UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.F and not gameProcessedEvent then -- use gameProcessedEvent to check if the player is using chat/interacting with another gui
        if not Bind then
            Bind = true
            BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

            while Bind do -- change made here
                game.Players.LocalPlayer.Backpack.XP.Value += 20 -- this won't replicate to the server, you will have to use remote events for this
                wait(3)
            end
        else
            Bind = false
            BV.MaxForce = Vector3.new()
        end
    end
end)
1 Like

Thanks it worked, ill reply to you if I have any other problems

oh wait, i might gotta redo my datastore but its not saving.