UIS = game:GetService("UserInputService")
local Seat = script.Parent -- Path to seat
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if Seat.Occupant ~= nil then
local Char = Seat.Occupant.Parent
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
while task.wait() do -- a loop
game.Workspace.TrainKit1.Base.AssemblyLinearVelocity = script.Parent.CFrame.LookVector*-15
end
while task.wait() do -- a loop
game.Workspace.TrainKit2.Base.AssemblyLinearVelocity = script.Parent.CFrame.LookVector*-15
end
end
end)
end
end)
UserInputService must be called in a local script parented to a player, it appears your local script is a child of the seat of the tram, not of the player. Put the script in StarterPlayer/StarterPlayerScripts, redo the paths, and it should work (assuming the rest of the code is correct)
Your second loop won’t run after the first one. Why use separate loops? Also, the UserInputService.InputBegan connection should not be nested into the PropertyChanged connection.
Hi. Thanks for giving that info. But after making these changes, the tram goes without input
UIS = game:GetService("UserInputService")
local Seat = game.Workspace.ControllerSeat -- Path to seat
if Seat.Occupant ~= nil then
local Char = Seat.Occupant.Parent
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
while task.wait() do -- a loop
game.Workspace.TrainKit1.Base.AssemblyLinearVelocity = game.Workspace.TrainKit1.Base.CFrame.LookVector*-15
game.Workspace.TrainKit2.Base.AssemblyLinearVelocity = game.Workspace.TrainKit2.Base.CFrame.LookVector*-15
end
end
end)
end
local UIS = game:GetService("UserInputService")
local Seat = game.Workspace:WaitForChild("ControllerSeat") -- Path to seat
if Seat.Occupant ~= nil then
local Char = Seat.Occupant.Parent
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
game.Workspace.TrainKit1.Base.AssemblyLinearVelocity = game.Workspace.TrainKit1.Base.CFrame.LookVector*-15
game.Workspace.TrainKit2.Base.AssemblyLinearVelocity = game.Workspace.TrainKit2.Base.CFrame.LookVector*15
end
end)
end
local Seat = -- please redo the path to the seat; game.Workspace.Tram.Seat for example
local player = game:GetService("Players").LocalPlayer
local occupant -- declare the occupant outside so we can use it in both functions
Seat:GetPropertyChangedSignal("Occupant"):Connect(function() -- this function just gets the occupant
if Seat.Occupant ~= nil then
occupant = Seat.Occupant.Parent
end
end)
local isMoving = false
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.W and occupant == player.Character then -- check our localplayer is in the seat
isMoving = true
end
end)
UIS.InputEnded:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.W and occupant == player.Character then
isMoving = false
end
end)
while task.wait() do -- before you had done two infinite loops after the other, this wouldn't work
if isMoving then
game.Workspace.TrainKit1.Base.AssemblyLinearVelocity = script.Parent.CFrame.LookVector*-15
game.Workspace.TrainKit2.Base.AssemblyLinearVelocity = script.Parent.CFrame.LookVector*-15 -- so we combine them into one
else
game.Workspace.TrainKit1.Base.AssemblyLinearVelocity = 0 -- if there's no input then we'd want it to stop, correct?
game.Workspace.TrainKit2.Base.AssemblyLinearVelocity = 0
end
end
This is really just a basic script, let me know if it does what you roughly needed it to do. Bare in mind, please put this script in StarterPlayerScripts or anywhere that a localscript would run. You can change the values of the speed etc.