Need help with script

I have a 0 gravity game, and I want to make a script that makes it feel like you are using thrusters rather than walking. I have a script that makes your walkspeed and jumpheight 0 when you join. My problem is that the thruster script looks fine, and i’m not getting anything in output, but the script doesnt work at all.

Here is the code:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

-- Disable walking and jumping
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0

local isThrusting = false

-- Define your custom settings
local maxThrustForce = 100 -- Adjust the maximum thrust force as needed
local accelerationRate = 5 -- Rate of acceleration

-- Function to handle thrusting
local function applyThrust()
    if isThrusting then
        local forwardVector = character.CFrame.lookVector
        local rightVector = character.CFrame.rightVector
        local upVector = character.CFrame.upVector

        local thrustDirection = Vector3.new(0, 0, 0)

        if input.W then
            thrustDirection = thrustDirection + forwardVector
        end
        if input.S then
            thrustDirection = thrustDirection - forwardVector
        end
        if input.D then
            thrustDirection = thrustDirection + rightVector
        end
        if input.A then
            thrustDirection = thrustDirection - rightVector
        end
        if input.Space then
            thrustDirection = thrustDirection + upVector
        end
        if input.LeftShift then
            thrustDirection = thrustDirection - upVector
        end

        thrustDirection = thrustDirection.Unit * maxThrustForce

        -- Apply a force to the HumanoidRootPart
        character.HumanoidRootPart.Velocity = thrustDirection
    end
end

-- Detect when a key is pressed and released
local input = {}

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if not gameProcessedEvent then
        if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.S or
           inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.D or
           inputObject.KeyCode == Enum.KeyCode.Space or inputObject.KeyCode == Enum.KeyCode.LeftShift then
            input[inputObject.KeyCode] = true
            isThrusting = true
        end
    end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
    if not gameProcessedEvent then
        if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.S or
           inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.D or
           inputObject.KeyCode == Enum.KeyCode.Space or inputObject.KeyCode == Enum.KeyCode.LeftShift then
            input[inputObject.KeyCode] = false
            isThrusting = false
        end
    end
end)

-- Listen for constant thrusting
while true do
    wait(0.1) -- Adjust this delay as needed for your desired rate of acceleration
    applyThrust()
    print("Thrusting:", isThrusting) -- Debugging message
    print("Velocity:", character.HumanoidRootPart.Velocity) -- Debugging message
end

This is inside of the player model in the workspace. ’
If you can help me with this, you are a gigachad

1 Like

Where is it? As it sounds like it should be in StarterCharacterScripts to start off. Idk what you mean playermodel in workspace, as that gets added upon join

input is a local variable, which means calling it in the function above it probably is the cause (move it above the applyThrust() function), also I recommend you use task.wait() from the task library instead of the old wait().

im using a script that adds it to the player model when it appears in the world. So its not just when they join, but when they respawn too.

it sounds like you think the code would be better written like this

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:FindFirstChild("HumanoidRootPart")

-- Disable walking and jumping
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0

local isThrusting = false

-- Define your custom settings
local maxThrustForce = 100 -- Adjust the maximum thrust force as needed
local accelerationRate = 5 -- Rate of acceleration

local forwardVector = rootPart.CFrame.lookVector
local rightVector = rootPart.CFrame.rightVector
local upVector = rootPart.CFrame.upVector

local thrustDirection = Vector3.new(0, 0, 0)

if input.W then
	thrustDirection = thrustDirection + forwardVector
end
if input.S then
	thrustDirection = thrustDirection - forwardVector
end
if input.D then
	thrustDirection = thrustDirection + rightVector
end
if input.A then
	thrustDirection = thrustDirection - rightVector
end
if input.Space then
	thrustDirection = thrustDirection + upVector
end
if input.LeftShift then
	thrustDirection = thrustDirection - upVector
end

-- Function to handle thrusting
local function applyThrust()
	if isThrusting then

		thrustDirection = thrustDirection.Unit * maxThrustForce

		-- Apply a force to the HumanoidRootPart
		character.HumanoidRootPart.Velocity = thrustDirection
	end
end

-- Detect when a key is pressed and released
local input = {}

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.S or
			inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.D or
			inputObject.KeyCode == Enum.KeyCode.Space or inputObject.KeyCode == Enum.KeyCode.LeftShift then
			input[inputObject.KeyCode] = true
			isThrusting = true
		end
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.S or
			inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.D or
			inputObject.KeyCode == Enum.KeyCode.Space or inputObject.KeyCode == Enum.KeyCode.LeftShift then
			input[inputObject.KeyCode] = false
			isThrusting = false
		end
	end
end)

-- Listen for constant thrusting
while true do
	wait(0.1) -- Adjust this delay as needed for your desired rate of acceleration
	applyThrust()
	print("Thrusting:", isThrusting) -- Debugging message
	print("Velocity:", character.HumanoidRootPart.Velocity) -- Debugging message
end

Also, i keep getting an attempt to index nil with W error in output when i do this.