Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use a RemoteEvent? (Answered)

Asked by 8 years ago

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

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. Legojoker 345 — 8y
0
You do know that you don't need RemoteEvents if you have FE off? alphawolvess 1784 — 8y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

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.

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 lukeb50 631 — 8y
Ad

Answer this question