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