Jetpackscript doesnt work

Hello, im making a jetpack script. For now, im making it without model.
But I have a problem. Velocity doesnt work and inputEnded event doesnt work.
This is what i have.
robloxapp-20200613-1556119.wmv (1.4 MB)

Here is code:

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
	--//Variables\\--
local key = 'Q'	
	local humroot = char:FindFirstChild("HumanoidRootPart")
	--//Services\\--
	
	local UIS = game:GetService("UserInputService")
	local canThrust = false
	
	UIS.InputBegan:Connect(function(input, isTyping)
		isTyping = false
		if input.KeyCode == Enum.KeyCode[key] then
			canThrust = true
			isTyping = true
			if isTyping == true and canThrust == true then
			local BV = Instance.new("BodyVelocity", humroot)
			BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			BV.Velocity = humroot.CFrame.lookVector * 100
				
				UIS.InputEnded:Connect(function()
					if input.KeyCode == Enum.KeyCode[key] then
						isTyping = false
						canThrust = false
						BV:Destroy()
					end
				end)
			end
		end
	end)
end)

end)

Have you tried debugging to check where it stops working? Also, you’re checking if the game proccessed the event. (if the player chatted etc)

Also, you’re doing this with a serverscript, userinputservice only works on the client, consider using remotevents.

On output there is no error. And need help with input ended. How can i detect if player is holding the [key]?
And thanks for serverscript idea!

There’s no error because it just doesn’t run.

local JetpackEvent = game.ReplicatedStorage.JetpackEvent

local plr = game.Players.LocalPlayer

local char = plr.Character --Error is here. It says : ServerScriptService.JetpackScriptGlobal:3: attempt to index nil with ‘Character’.

local canThrust = false

local humroot = char:FindFirstChild(“HumanoidRootPart”)

JetpackEvent.OnServerEvent:Connect(function()

local BV = Instance.new(“BodyVelocity”, humroot)

BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

BV.Velocity = humroot.CFrame.LookVector*60

wait(0.6)

BV:Destroy()

end)

When i try to run, its says tried to index nil with character

wait did you put this code in a local script or normal script. you cant do game.Players.LocalPlayer in a normal script only local script

Why are you creating a listener for the player? UserInputService only works on the client, so I’m assuming this is a LocalScript. You don’t need to set up a listener on the client. I would put this in StarterCharacterScripts and get the Player/Character with

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

Also, by creating the InputEnded listener inside of InputBegan, you will be creating a new listener every single Q key press, which will create memory leaks. These functions should be separate of each other, with canThrust being a global variable.

And I’m not sure why you’re manually setting isTyping because it will return a useful bool that you’re basically rendering useless.

With those suggestions, I quickly rewrote your code

LocalScript inside of StarterCharacterScripts:

--//Services\\--
local UIS = game:GetService("UserInputService")

--//Variables\\--
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humroot = char.HumanoidRootPart
local key = 'Q'
local canThrust = false -- make this global so the UIS listeners can still interact


--//Listeners\\--
UIS.InputBegan:Connect(function(input, isTyping) -- create body velocity
	if input.KeyCode == Enum.KeyCode[key] and not isTyping then
		canThrust = true
		
		local BV = Instance.new("BodyVelocity")
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BV.Velocity = humroot.CFrame.lookVector * 100
		BV.Parent = humroot
	end
end)


UIS.InputEnded:Connect(function(input, isTyping) -- remove body velocity
	if input.KeyCode == Enum.KeyCode[key] and not isTyping then
		canThrust = false
		local BV = humroot:FindFirstChildOfClass("BodyVelocity") -- make sure BV exists
		if BV then
			BV:Destroy()
		end
	end
end)
2 Likes