Connecting Server-side and client-side input into same event?

image

Would this suffice instead of an if-statement?

It is more ethical to add something like this.

player.Chatted(msg)
if player.Name == your username then

This would prevent errors as your player object may not be found sometimes, as you won’t always be in game.

msg is undefined with that if-statement.

game.Players.PlayerAdded:Connect(function(player)
     player.Chatted:Connect(function(msg)
        if player.Name == your username then
          --command execution
  end
 end)
end)

This will get the new player, when said player chats it will ensure that the username of said user is yours, if it is, then it will proceed with your command handler.

This doesn’t appear to work for some reason, although I receive no error in output.

Screenshot of the entire thing please.

image

Including command execution please.

I can’t screenshot 150 lines of code

Try going through and adding print functions after PlayerAdded, Chatted, and if-statement, see if there is any time that it does not print.

I receive nothing in output. I’m not sure where the code has gone awry.

Nothing, even after adding print functions?

I added three print functions that should print out A, B, and C depending on its location. I received none of them, for some odd reason.

Sometimes the events do not register, try copying your command handler, deleting it, then delete all the events and functions. Go where I posted my code, and type in it manually, do not copy and paste it. After that, you can paste your command handler.

If that does not work, make sure the events are at the top of your script, so no yielding functions are stopping it from running.

@Celestagra I would check to see if the event is being fired before you assign a connection. Especially with the case of PlayerAdded, I add this redundancy check:

function newPlayer(player)
 player.Chatted:Connect(function(msg)
  if player.Name == "Celestagra" then
          --command execution
  end
 end)
end

for i,player in pairs(game:GetService("Players"):GetPlayers()) do
	newPlayer(player)
end

game:GetService("Players").PlayerAdded:Connect(newPlayer)

This applies the function for any existing players in the game (people who joined before PlayerAdded was setup). So I’d check to see if adding this works out.

1 Like

Hey there,
Regardless of what your code is attempting to achieve, the specific networking problem you described can be solved by simply defining your server-sided function with a variable, and then you can connect it to as many different listeners as you want. Right now your function is “anonymous”, which means it is not defined with a variable.

Once you’ve defined it, connect it once to your RemoteEvent, and then to your BindableEvent.
RemoteEvents are strictly for usage between Clients and the Server. BindableEvents can only be fired on just the Server, or just the Client.

-- on the server
local yourFunction = function(player, arg)
-- perform the action
end

Relay.RemoteEvent.OnServerEvent:connect(yourFunction)
Relay.BindableEvent.Event:connect(yourFunction)

And so when you want to trigger this function from the Server in any Script, you’ll say

Relay.BindableEvent:Fire(somePlayer,arg)
1 Like