Why does this code not work?

The code is to make a controllable tram

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

Is this in a regular script or a local script?

1 Like

It is a regular script. Should its be a local script?

1 Like

Yes, UserInputService can only be used in a local script.

1 Like

https://developer.roblox.com/en-us/api-reference/class/UserInputService

1 Like

I just changed it into a LocalScript and found that that the “tram” still did not move. Can you see any other reasons this would not work?

1 Like

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)

1 Like

Alright. I’ll give it a crack and update you.

1 Like

Wait. You used a while loop which runs infinetly, so the second TrainKit2 will not work

1 Like

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.

1 Like

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

What could be wrong with this code?

2 Likes

Try adding this right above the if statement

if input.UserInputType == Enum.UserInputType.KeyBoard then

1 Like

The tram still goes without input?

1 Like

Just do this:

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
1 Like

Try this:

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.

3 Likes

The tram/train still moves without input, even when deleting my code and replacing w/ yours, i am genuinely very puzzled right now

Thank you so much! Much gratitude towards you and I wish you well.

1 Like

What @bubbavimto did is what I meant :slight_smile: