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.
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)
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
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)