This script is supposed to insert BodyGyro and BodyVelocity instances after a key is pressed and held. The user's body is tracking the mouse of the user. However, after you do it the first time, the body doesn't track the mouse any longer.
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local KeyPressed = false
repeat wait() until Player.Character
c = Player.Character
local Time = 0
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.F then
KeyPressed = true
local BG = Instance.new("BodyGyro", c.HumanoidRootPart)
BG.maxTorque = Vector3.new(250000,250000,250000)
BG.CFrame = CFrame.new(c.HumanoidRootPart.Position, mouse.hit.p)
BG.P = 20000
local BP = Instance.new("BodyPosition", c.HumanoidRootPart)
BP.maxForce = Vector3.new(math.huge, math.huge, math.huge)
BP.Position = c.HumanoidRootPart.Position
while KeyPressed == true and Time < 60 do
wait()
print("Key is being held")
BG.CFrame = CFrame.new(c.HumanoidRootPart.Position, mouse.hit.p)
Time = Time + 1
end
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
KeyPressed = false
if KeyPressed == false then
BG:Remove()
BP:Remove()
end
end
end)
end
end)
What could the issue be?

1 comment
thanks!