I am trying to make a boat in my game. When I press the buttons needed to use a function, it does nothing. In the local script:
local MoveEvent = script.Parent.RemoteEvent local uis = game:GetService("UserInputService") uis.InputBegan:connect(function(Input) local KeyCodeName = Input.KeyCode.Name if KeyCodeName == "W" then MoveEvent:FireServer("Forwards") elseif KeyCodeName == "S" then MoveEvent:FireServer("Backwards") elseif KeyCodeName == "A" then MoveEvent:FireServer("Left") elseif KeyCodeName == "D" then MoveEvent:FireServer("Right") end end) uis.InputEnded:connect(function(Input) local KeyCodeName = Input.KeyCode.Name if KeyCodeName == "W" or "Up" or "S" or "Down" then MoveEvent:FireServer("StopMove") elseif KeyCodeName == "A" or "Left" or "D" or "Right" then MoveEvent:FireServer("StopTurn") end end)
In the script:
script.Parent.RemoteEvent.OnServerEvent:connect(function() print("hi") end)
When I try to use it, It gives no errors
RemoteEvents go in ReplicatedStorage
. The server connects to OnServerEvent
and the first argument passed is the Player of the client who fired it.
Your other problem is in the if statements.
KeyCodeName == "W" or "Up" or "S" or "Down"
does not mean what you think it means. There are four separate conditions being evaluated here, and if any one of them is true then the entire statement is true. These four are KeyCodeName == "W"
, which can vary, and "Up"
, "S"
, "Down"
which are all true and cannot vary. What you mean to do is check if KeyCodeName is equal to any of those values, and not check if it is equal to "W" or some strings exist. In that case, write
if KeyCodeName=="W"or KeyCodeName=="Up"or KeyCodeName=="S"or KeyCodeName=="Down"then
for that particular statement. The next statement has the same problem.