I am trying to make a swing. This swing can be pushed by holding the W and S keys. However, the swing can still be pushed when i'm out of the seat.
local seat = script.Parent
seat.Changed:Connect(function(property)
if property ~= "Occupant" then return end
local occupant = seat.Occupant
if occupant then
local character = occupant.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
print(player.Name .. " is sitting")
local keyHeld = false
local thruster = Instance.new("BodyThrust", seat)
local function keyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.W then
keyHeld = true
while keyHeld do
thruster.Force = Vector3.new(0,0,-500)
wait()
end
end
if inputObject.KeyCode == Enum.KeyCode.D then
keyHeld = true
while keyHeld do
thruster.Force = Vector3.new(0,0,500)
wait()
end
end
end
local function keyRelease(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.W then
keyHeld = false
thruster.Force = Vector3.new(0,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.D then
keyHeld = false
thruster.Force = Vector3.new(0,0,0)
end
end
game:GetService("UserInputService").InputBegan:Connect(keyPress)
game:GetService("UserInputService").InputEnded:Connect(keyRelease)
end
end
end)
Any ideas?
