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

After key has been pressed first time, a part of this script ceases to work..?

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 answer

✓ Accepted answer

You forgot to set time to 0 if the player releases the key.

UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = false
    if KeyPressed == false then
	Time = 0
        BG:Remove()
        BP:Remove()
                end
            end
        end)
    end
end)

1 comment

0

thanks!

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