Seat Forward Script

I just tried to make something simple where if the player is occupying a seat and holds W it moves forward. I don’t want it to move based on the the direction the players camera is facing or humanoid movement, just holding W means that the seat moves forward. It doesn’t work and I cant for the life of me figure out why. And ideas? Thanks.

local seat = script.Parent
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local speed = 16
local moving = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if not gameProcessed then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            if input.KeyCode == Enum.KeyCode.W then
                if seat.Occupant then
                    moving = true
                end
            end
        end
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.W then
            moving = false
        end
    end
end)

RunService.RenderStepped:Connect(function(deltaTime)
    if seat.Occupant and moving then
        local currentCFrame = seat.CFrame
        local forwardVector = currentCFrame.LookVector
        local movementDelta = forwardVector * speed * deltaTime
        seat.CFrame = currentCFrame + movementDelta
    end
end)

Please help, thanks again!

4 Likes

Can’t you just do:

seat.Position += seat.CFrame.LookVector * speed * deltaTime

?

1 Like

I tried this and it does not work. Any idea why?

local seat = script.Parent
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local speed = 16
local moving = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed and input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.W and seat.Occupant then
		moving = true
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.W then
		moving = false
	end
end)

RunService.RenderStepped:Connect(function(deltaTime)
	if seat.Occupant and moving then
		seat.Position += seat.CFrame.LookVector * speed * deltaTime
	end
end)

Ah, I see, local script must be inside the player or character (StarterPlayerScripts and StarterCharacterScripts will put your local scripts inside the PlayerScripts and Player Character respectively). UserInputService and RunService.RenderStepped are only accessible through the client.

1 Like

Wow, thanks man. It works great now! Thanks a lot.

1 Like