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

My event is not firing? I don't understand Events.

Asked by
Galicate 106
6 years ago
Edited 6 years ago

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)
0
Include the server script hellmatic 1523 — 6y
0
...What.... Galicate 106 — 6y
0
lol you need two seperate scripts to use remotes. One is a Local Script (client) and the other one is a regular script (server script). Watch tutorials on YouTube so you are familiar. Once you get the concept, you will understand waaay easier. awesomeipod 607 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Instance.new("RemoteEvent") doesn't work with remote events. This is the correct way to use them:

  1. 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")

  2. 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!

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
Learn this first. Once you get the hang of it, you can better understand how you can use :FireAllClients(), :FireClient(), etc. awesomeipod 607 — 6y
0
Notice how both scripts are linked with the SAME remote event. If they are not the same remote event, they can't communicate with each other. awesomeipod 607 — 6y
0
Ok, thanks... The wiki confuses me. Galicate 106 — 6y

Answer this question