Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you use a RemoteEvent? (Answered)

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

2 comments

0

The local script and the server script seem to have the same parent. That should never happen; that means the local script is either not in a player's Character/GUI/Backpack or the server script is not in Workspace/ServerScriptService.

0

You do know that you don't need RemoteEvents if you have FE off?

1 answer

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.

1 comment

0

no need to have them in repstorage. @Legojoker has a good answer to the problem, both scripts must be inside the event for it to work,creating an impossible situation

This discussion is preserved as it was. New answers, comments, and votes are closed.