Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I don't understand why this script does this when sending information through remoteEvents?

  1. What do you want to achieve?

So, I want it so that when I click a button it would display a small thumbnail size head image of the player on everyone's screen. This image would also have the name of the player. I want this image to be positioned in a certain location on the screen which has also been done.

  1. What is the issue?

The problem is, when the button is clicked I've managed to display an image, but the image that it displays is that of the local player. If one person clicks the button, it displays an image of each of the players on their own screen. What I want it to do is that when one player clicks the button on the Gui, an image of that exact client will show on every other screen, but it doesn't. I don't understand how I would do this with the code that I've got.

  1. What solutions have you tried so far?

I've already asked a question previously that was similar to this, but since I've asked the question my code has changed a lot. I've looked at every section I possibly could and still haven't found an answer.

Code:

Local script:

--local remoteFunction = game.ReplicatedStorage.remoteFunction
local pictureRemoteEvent = game.ReplicatedStorage.pictureRemoteEvent

--event for when the button is clicked to start the race and enter the player

script.Parent.readyToStart.MouseButton1Click:Connect(function(player)
	pictureRemoteEvent:FireServer(player)
end)

--creating the image for all the players
pictureRemoteEvent.OnClientEvent:Connect(function()
	--setting up the image label
	print(player.Name)
	--local remoteEventLocalPhoto = game.ReplicatedStorage.robloxLocalPhoto
	local typingChallenge = player.PlayerGui.typingChallenge
	local otherRacePosition = typingChallenge.otherRacePosition
	local newImage = Instance.new("ImageLabel", otherRacePosition)
	newImage.Name = player.Name
	newImage.Position = UDim2.new(0, 0,0, 0)
	newImage.Size = UDim2.new(0.06, 0,0.998, 0)
	newImage.BackgroundTransparency = 1
	
	-- setting the image label to the player's head
	local UserId = player.UserId
	local thumbType = Enum.ThumbnailType.HeadShot
	local thumbSize = Enum.ThumbnailSize.Size100x100
	local players = game:GetService("Players")
	local content, isReady = players:GetUserThumbnailAsync(UserId, thumbType, thumbSize)
	newImage.Image = content
end)

Server script:

local pictureRemoteEvent = game.ReplicatedStorage.pictureRemoteEvent

pictureRemoteEvent.OnServerEvent:Connect(function(player)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		pictureRemoteEvent:FireClient(player)
	end
end)

1 answer

✓ Accepted answer

You just need a few changes to how you are passing and using the player variable:

Local script:

--event for when the button is clicked to start the race and enter the player
local pictureRemoteEvent = game.ReplicatedStorage.pictureRemoteEvent

script.Parent.readyToStart.MouseButton1Click:Connect(function()
	-- Remote events already pass the sending player as the first argument to the server, so you don't need to manually send it here
	pictureRemoteEvent:FireServer()
end)

--creating the image for all the players
-- The server passes us the player that clicked, so you need to add it on the next line as a function argument if you want to be able to access it
pictureRemoteEvent.OnClientEvent:Connect(function(player)
	--setting up the image label
	print(player.Name)
	--local remoteEventLocalPhoto = game.ReplicatedStorage.robloxLocalPhoto
	-- You want the local player's PlayerGui here
	local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
	local typingChallenge = playerGui.typingChallenge
	local otherRacePosition = typingChallenge.otherRacePosition
	local newImage = Instance.new("ImageLabel", otherRacePosition)
	newImage.Name = player.Name
	newImage.Position = UDim2.new(0, 0,0, 0)
	newImage.Size = UDim2.new(0.06, 0,0.998, 0)
	newImage.BackgroundTransparency = 1

	-- setting the image label to the player's head
	local UserId = player.UserId
	local thumbType = Enum.ThumbnailType.HeadShot
	local thumbSize = Enum.ThumbnailSize.Size100x100
	local players = game:GetService("Players")
	local content, isReady = players:GetUserThumbnailAsync(UserId, thumbType, thumbSize)
	newImage.Image = content
end)

Server script:

local pictureRemoteEvent = game.ReplicatedStorage.pictureRemoteEvent

pictureRemoteEvent.OnServerEvent:Connect(function(player)
	-- You are also sending this to the player that clicked the button in the loop below. If you didn't want to do this you would need to check that p ~= player
	for _, p in pairs(game:GetService("Players"):GetPlayers()) do
		-- The first argument to FireClient is the player that you want to send to, any arguments after that get passed with the event to that player. So here, we're sending player p the player that clicked the button
		pictureRemoteEvent:FireClient(p, player)
	end
end)

I haven't tested this, but it should do what you need.

This discussion is preserved as it was. New answers, comments, and votes are closed.