It seems that my events arent firing because I am not seeing print("Event Fired")
in the output. I don't understand Events, but I want a gui to change for every player.
-----------------------Events--------------------------------------- local RecruitChosen = Instance.new("RemoteEvent") local StrikerChosen = Instance.new("RemoteEvent") local DecoyChosen = Instance.new("RemoteEvent") local CaliaChosen = Instance.new("RemoteEvent") local BlasterChosen = Instance.new("RemoteEvent") RecruitChosen.Parent = game.ReplicatedStorage StrikerChosen.Parent = game.ReplicatedStorage DecoyChosen.Parent = game.ReplicatedStorage CaliaChosen.Parent = game.ReplicatedStorage BlasterChosen.Parent = game.ReplicatedStorage RecruitChosen.Name = "RecruitWasChosen" StrikerChosen.Name = "StrikerWasChosen" DecoyChosen.Name = "DecoyWasChosen" CaliaChosen.Name = "CaliaWasChosen" BlasterChosen.Name = "BlasterWasChosen" -----------------------Player Models--------------------------------------- local RecruitModel = game.ServerStorage.Recruit local StrikerModel = game.ServerStorage.Striker local DecoyModel = game.ServerStorage.Decoy local CaliaModel = game.ServerStorage.Calia local BlasterModel = game.ServerStorage.Blaster local Player = game.Players.LocalPlayer function Recruit() print("Clicked") if script.Parent.Recruit.PlayerSelected.Text == "None" then RecruitChosen:FireServer() local ClonedModel = RecruitModel:Clone() ClonedModel.Parent = workspace Player.Character = ClonedModel workspace.CurrentCamera:Destroy() wait() workspace.CurrentCamera.CameraType = "Custom" workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character[("Humanoid")] end end function Striker() print("Clicked") if script.Parent.Striker.PlayerSelected.Text == "None" then StrikerChosen:FireServer() local ClonedModel = StrikerModel:Clone() ClonedModel.Parent = workspace Player.Character = ClonedModel workspace.CurrentCamera:Destroy() wait() workspace.CurrentCamera.CameraType = "Custom" workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character[("Humanoid")] end end function Decoy() print("Clicked") if script.Parent.Decoy.PlayerSelected.Text == "None" then DecoyChosen:FireServer() local ClonedModel = DecoyModel:Clone() ClonedModel.Parent = workspace Player.Character = ClonedModel workspace.CurrentCamera:Destroy() wait() workspace.CurrentCamera.CameraType = "Custom" workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character[("Humanoid")] end end function Calia() print("Clicked") if script.Parent.Calia.PlayerSelected.Text == "None" then CaliaChosen:FireServer() local ClonedModel = CaliaModel:Clone() ClonedModel.Parent = workspace Player.Character = ClonedModel workspace.CurrentCamera:Destroy() wait() workspace.CurrentCamera.CameraType = "Custom" workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character[("Humanoid")] end end function Blaster() print("Clicked") if script.Parent.Blaster.PlayerSelected.Text == "None" then BlasterChosen:FireServer() local ClonedModel = BlasterModel:Clone() ClonedModel.Parent = workspace Player.Character = ClonedModel workspace.CurrentCamera:Destroy() wait() workspace.CurrentCamera.CameraType = "Custom" workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character[("Humanoid")] end end -----------------------Fired Events--------------------------------------- function PlayerRecruit() print("Event Fired") script.Parent.Recruit.PlayerSelected.Text = "Already Taken" end function PlayerStriker() print("Event Fired") script.Parent.Striker.PlayerSelected.Text = "Already Taken" end function PlayerDecoy() print("Event Fired") script.Parent.Decoy.PlayerSelected.Text = "Already Taken" end function PlayerCalia() print("Event Fired") script.Parent.Calia.PlayerSelected.Text = "Already Taken" end function PlayerBlaster() print("Event Fired") script.Parent.Blaster.PlayerSelected.Text = "Already Taken" end -----------------------Connect Functions--------------------------------------- game.ReplicatedStorage.RecruitWasChosen.OnClientEvent:connect(PlayerRecruit) game.ReplicatedStorage.StrikerWasChosen.OnClientEvent:connect(PlayerStriker) game.ReplicatedStorage.DecoyWasChosen.OnClientEvent:connect(PlayerDecoy) game.ReplicatedStorage.CaliaWasChosen.OnClientEvent:connect(PlayerCalia) game.ReplicatedStorage.BlasterWasChosen.OnClientEvent:connect(PlayerBlaster) script.Parent.Recruit.MouseButton1Click:connect(Recruit) script.Parent.Striker.MouseButton1Click:connect(Striker) script.Parent.Decoy.MouseButton1Click:connect(Decoy) script.Parent.Calia.MouseButton1Click:connect(Calia) script.Parent.Blaster.MouseButton1Click:connect(Blaster)
Instance.new("RemoteEvent")
doesn't work with remote events. This is the correct way to use them:
In the explorer on studio, create a RemoteEvent and parent it somewhere, preferably ReplicatedStorage. (Do note: DO NOT parent it to ServerStorage or ServerScriptService. Everything inside these services will not be visible to the client, and thus, if you subscribe to an event on the client, it will be recognized as "nil")
Good. Now, the way you are going to use the remote event depends on whether it sends a message to the client or to the server.
For example purposes, lets say I made a remote event called "TestEvent", and placed it in ReplicatedStorage.
Sending a message to the server
If i wanted "TestEvent" to be the client sending a message to the server, first I would need to make a server script and subscribe to the remote event.
Subscribing to the remote event will give you these parameters: The player who fired the remote event, and a "Tuple" class, which is basically signaling many parameters.
For example, lets say I wanted to change the brick color of a door.
game.ReplicatedStorage.TestEvent.OnServerEvent:connect(function(plr,color) --changing the door color end)
As you can see, using the tuple class we added an extra parameter (You can add as much parameters as you want, as long as the player parameter is first!)
Now, you may be asking how to actually make the client communicate with the server, and that is very simple
game.ReplicatedStorage.TestEvent:FireServer(BrickColor.new("Brown"))
Whenever you fire to the server, all you need to include is the tuple arguments. You don't need the player argument, as that is already handled by roblox.
Sending a message to the client
Let's say "TestEvent" is going to tell the client to open a GUI. Now, things will be different, and I will go over those changes.
When we subscribe to the event (In a LocalScript), we will use this subscription instead:
game.ReplicatedStorage.TestEvent.OnClientEvent:connect(function(gui) --open the GUI end)
We don't need to include the "plr" argument this time, as we can just use "game.Players.LocalPlayer".
Now, if we want to fire to the client from the server, we will use this line of code:
game.ReplicatedStorage.TestEvent:FireClient(plr,"TheGui")
This time, we HAVE to include the "plr" argument, as the server will need to know what client will experience that change. There is also another command:
game.ReplicatedStorage.TestEvent:FireAllClients("TheGui")
:FireAllClients() doesn't need that "plr" argument, because the server will know to make every single client currently on the server experience that change. This can be useful for stuff like sending a message to the chat, a cutscene in some minigame game, etc.
I hope you understand how to work these events now. If not, feel free to ask me more!
Here is a simple example of how to send data from client to server.
Lets say there is a RemoteEvent
located in ReplicatedStorage
, named "Print".
Inside the client (local script):
local PrintEvent = game.ReplicatedStorage:WaitForChild('Print') -- start off by getting the remote local Text = 'test' -- this is what we will be sending to the server function Print() PrintEvent:FireServer(Text) -- we want to use FireServer() since we are sending data to the server end
Inside the server (regular script):
local PrintEvent = game.ReplicatedStorage:WaitForChild('Print') PrintEvent.OnServerEvent:connect(function(player, text) -- remember that the first parameter is ALWAYS the clients name who fired the remote print(player.Name .. " sent " .. text) -- output should show: 'name has sent test' end)