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

How to fire different functions in a RemoteEvent?

Asked by 6 years ago
Edited 6 years ago

Is it possible to fire different functions in a remote event and how would I go by doing it? For example; You want to change the color of your character, you have 5 different buttons that have different colors, but all the colors are stored in a function in a remote event.

3 answers

Log in to vote
0
Answered by 6 years ago

The localscript should send over some unique piece of information as an argument to the RemoteEvent:

  • The colour the user wanted (though this is vulnerable to exploiters choosing their own colours, unless you double check to make sure it's a valid colour)
  • Which button they pressed (ex "1" or "3")

The server script receives that piece of information as an argument and can act on it appropriately. You should probably put the colours in a table so that you can index it with the button number. ex:

colors = {Color3.new(1, 1, 0), Color3.new(0, 0, 0), --etc
}
--in function, say "buttonNum" is 2, then you can do:
ApplyColor(colors[buttonNum])

--then you just have to have 1 function, ApplyColor, which accepts the colour with which to colour the player

--If you need 5 different functions, you can put them in the 'colors' table instead:

colorFuncs = {ColorPlayer1, ColorPlayer2, --etc
}
--In function:
local func = colorFuncs[buttonNum]
func()
Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
Edited 6 years ago

Add a RemoteEvent in ReplicatedStorage named, "ColorChange", your choice.

Add a listener script in the ServerScriptService named, "[SS]Color". I use [SS] as a abbreviation for 'ServerScript'.

In the [SS]Color script, you have to find the ColorChange RemoteEvent.

--[SS]Color script

local RS = game:GetService('ReplicatedStorage')
local ColorChangeEvent = RS:WaitForChild('ColorChange')

Now, you want to create a OnServerEvent for the Remote.

ColorChangeEvent.OnServerEvent:connect(function(player)-- always know that the first arguement is always the Player whos firing the event.
end)

Now in the local script, or client script. This is inside a TextButton.

local RS = game:GetService('ReplicatedStorage')
local ColorChangeEvent = RS:WaitForChild('ColorChange')

local Button = script.Parent

local CurrentColor -- this is the current color

Button.MouseButton1Click:connect(function() -- lets say you clicked the red button
    CurrentColor = "Bright red" -- changes the CurrentColor to 'Bright red'

--Now, you want to fire the ColorChangeEvent

ColorChangeEvent:FireServer(CurrentColor) -- this sends the CurrentColor's name to the server script
end)

Back to the [SS]Color ServerScript.

ColorChangeEvent.OnServerEvent:connect(function(player, CurrentColor)
    print(player.Name .." has clicked the " .. CurrentColor .. " button!"
    -- prints the Player's name who fired the ColorChangeEvent, and sends the CurrentColor from the local script to the [SS]Color ServerScript.
end)
0
That does not answer his question. StoleYourClothes 105 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Here, hope this helps. (Only an example).

game.ReplicatedStorage.RemoteEvent.OnServerEvent:connect(function(Player, Data)
if Data == "Red" then
print("Red")
-- Additional code (add function)

elseif Data == "Blue" then
print("Blue")
-- Additional code (add function)

elseif Data == "Green" then
print("Green")
-- Additional code (add function)
else
    return
    end
end)

game.ReplicatedStorage.RemoteEvent:FireServer("Red")

Answer this question