If i use FireServer("string") in a LocalScript, how do I detect if it said "string" or not

Before you continue, I would like to say that I’m VERY new to scripting and I almost know nothing about it. I know stuff about local scripts mostly.

I have this in a local script

script.Parent.Down.MouseButton1Click:Connect(function()
    replicatedStorage.StageTransfer:FireServer("down")
end)

script.Parent.Up.MouseButton1Click:Connect(function()
    replicatedStorage.StageTransfer:FireServer("up")
end)

And I have a script that does something when the remotevent fires saying “up” or “down”. My question is, how do I tell if it fired with “up” or “down”? I want something like this (This makes no sense, but this is what I want)

if firedEvent == "up" then
     print("pressed up")
end

if firedEvent == "down" then
     print("pressed down")
end
1 Like
RemoteEvent.OnServerEvent:Connect(function(player, direction)
if direction == "up" then
end

if direction == "down" then

end
end)

The “RemoteEvent” is the remote event you are trying to use. The “player” is the player that fired the remote event

You can pass information from the client to the server using parameters.

2 Likes

Just a small suggestion I would like to say, you could use elseif instead of two if statements for cleaner code (Just my preference) :slightly_smiling_face:

4 Likes

I think you should explain more, regardless, I will:

If the first statement is true, its code will execute. Also, if the second statement is true, its code will execute.

elseif will be executed if the first statement is not true.

More information: https://stackoverflow.com/questions/19946963/if-vs-else-if-vs-else-statements

This is redundant, if the first statement is true then the second statement can’t be true and vice versa, you can’t assign two values to a single argument in Lua.

Ex:

if a == 1 then
-- a can't equal 2
end

if a == 2 then
-- a can't equal 1
end

An elseif statement, and what @Dolphin_Worm did are virtually the same, one just uses the direct syntax to accomplish it’s goal.

Might I also suggest a switch statement in the case that OP is adding more functions in the future?

Ex;

local functionsTbl = {
    up = function()
        print("up")
    end;
    down = function()
        print("down")
    end;
    left = function()
        print("left")
    end;
    right = function()
        print("right")
    end;
}

RemoteEvent.OnServerEvent:Connect(function(player, direction)
    if direction and functionsTbl[direction] then
        functionsTbl[direction]()
    end
end)

They are more or less the same, but there is one subtle difference: in the first case, only one of the two statements can be executed while in the second case it is possible for both of them to be executed.

In this example, it does not matter since the two conditions are mutually exclusive, but it may become important if it is possible for both conditions to be true. In that case, “if else if” would only execute the first “if” statement and “if if” would execute them both in sequence.

1 Like