How do i send data from one client to another specific client?

Hi dev forum! I’m a beginner scripter (been scripting for 2 weeks) but I’m learning quickly!! :smiley:

You can write your topic however you want, but you need to answer these questions:
1. What do you want to achieve? Keep it simple and clear!
I’m scripting a group/team system for my survival game. Currently I’ve managed for each client to be able to create a server-sided team. a “group”. I’ve also scripted an empty team deleter that removes empty groups. The final step to complete my group system is to create a “send invite to other specific clients” function so that each group leader can invite friends and strangers on their team.

2. What is the issue? Include screenshots / videos if possible!
I can’t figure out how to send data from one client to another specific client. Here is an illustration:

Basically I’ve succeeded for each client that send an invite to reach THE SERVER but from there it ends, I can’t reach the specific client. Here is my local script for the team/group leader:
*Survivors is the neutral auto-assignable team on the server. All players that are not in Survivors have either created or joined a private team.

local RS = game:GetService("ReplicatedStorage")
local Frame = script.Parent
local players = game:GetService("Players")
local teams = game:GetService("Teams")
local playerList = players:GetPlayers()
local positionTable = {UDim2.new(0.154, 0, 0.054, 0), UDim2.new(0.154, 0, 0.180, 0), UDim2.new(0.154, 0, 0.310, 0),
	UDim2.new(0.154, 0, 0.440, 0), UDim2.new(0.154, 0, 0.570, 0)
}

function generateNames() -- generates a full list of all players on the server
	for i, v in pairs(playerList) do
		local localplayer = players.LocalPlayer
		local playerButton = Instance.new("TextButton")
		playerButton.Parent = Frame
		playerButton.Text = (v.Name)
		playerButton.Position = positionTable[i]
		playerButton.Size = UDim2.new(0, 69, 0, 20)
		playerButton.MouseButton1Up:Connect(function()
			if localplayer.Team == teams.Survivors then -- checks if the client is on the neutral team "Survivors". If it is then he/she has to create a group.
				print("You need to create a group")
				else
				RS:WaitForChild("SendInvite"):FireServer(v, localplayer) -- sends data to server, localplayer(the sender), v(the reciever)
				print("Invite sent")
			end
		end)
	end
end

Frame.SendMessage.MouseButton1Up:Connect(generateNames) -- ignore this it's just a button to generate the playerlist

Here is the server sided script that runs the remoteEvent:

local RS = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local playerList = players:GetPlayers()
local teams = game:GetService("Teams")

RS.SendInvite.OnServerEvent:Connect(function(localplayer, v)
	print("server recieved")
	print(localplayer) -- just checks to see if "sender" input has reached the server
	print(v) -- just checks to see if "specified reciever" input has reached the server
	wait(0.5)
	RS.sendInfoToclient:FireClient(v, localplayer) -- tries to port the data to a specific client, specifically "v". localplayer is also sent trough to identify the team later
		local newbutton = Instance.new("TextButton")
		newbutton.Parent = v.PlayerGui.ScreenGui -- places the "accept button" in the specified clients ui. The recievers ui basically.
		newbutton.Text = ("Accept invite")
		newbutton.Position = UDim2.new(0, 0, 0, 0)
		newbutton.Visible = true
		newbutton.MouseButton1Up:Connect(function() -- if player v clicks the button the following code will check:
			if v.Team == teams.Survivors then -- checks if player v is in the "neutral" team which is Survivors. if it is then player v can join the localplayers team
			v.Team = localplayer.Team -- places the player v (the client that info has been sent to) in the localplayers team (the client that sent the info)
			end
		end)
end)

I don't know if any of that makes sense or if i've pasted the code correctly.


**3. **What solutions have you tried so far?** Did you look for solutions on the Developer Hub?**
I've been in studio for hours, watched multible youtube vids and have also read a number of dev topics related to this issue. 

I feel like this is an eazy thing to script but I'm totally lost. Any suggestions/ ideas on how to do this is highly appreciated!! :smile:
2 Likes

You seem to have made a classic mistake.

When you fire any event to the server, even if it has no parameters, it automatically passes the player parameter.

Example:

--CLIENT

Event:FireServer()

--SERVER

Event.OnServerEvent:Connect(function(Player)

end

So what really happens with the arguments that you’re sending is as follows:

--CLIENT

Event:FireServer()

--SERVER

Event.OnServerEvent:Connect(function(Player, Receiver, Player) -- Mind you player is the same thing

end

You don’t need to send your own player over because it’s already passed as a parameter within the scope of the function. This doesn’t seem to be your main issue though.

When you fire the client, you don’t actually seem to be doing anything on the client. You’re creating the UI on the server (from what I can see), and then creating the clicked event on the server. While MouseButton events sometime work on the server, it’s not really good practice, and you should leave that to a client script. So make a .OnClientEvent for the client for that specific sendInfoToClient event, so it can create the button on the client. Then send the info back to the server with the click. Maybe using a RemoteFunction so you can yield that function with the data.

Hope this helps, feel free to ask any questions.

1 Like

Im going to test around abit with the .OnClientEvent and see if I can get it to work. If I undesrtand you correctly I should do the following:
Local script (for invoking a remoteevent) - to send the invite

Script (for assigning a function when sendInfoToclient.OnServerEvent is triggered. In this function I will trigger the removeEvent again but I will instead use RS.sendInfoToclient:FireClient(v)


another local script where I use RS.sendInfoToclient.OnClientEvent:Connect(function)
in this function I will make the accept button pop up. Therefor it should only appear for the specified client.

hopefully I understood you correctly, basically: FireServer → OnServerEvent → FireClient → OnClientEvent

Looks quite correct to me. Make sure you link the accept button to whatever you want it to do. Probably going to be another FireServer again.

yes! Finally! I did it!! Thank you so much for helping me Ben :smiley:
It’s still a bit confusing with all the parameters and arguments and which players to pass trough but Im guessing that i’ll get used to it eventuelly. thanks alot

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.