Gui not popping up and showing the stats

Ok so I know I have asked similar questions like this, but this time, I have a script. It is supposed to show an image label of a player, but it doesn’t. Please point out any errors I made, because there probably a ton…

 function GetPlayerWithMostKills()
  local current, plr = 0, nil
for i, v in pairs(game.Players:GetChildren()) do
  if v.leaderstats.Kills.Value > current then
  plr = v
  end
  end

  return plr
  end

  --when round ends
  PlayerWithMostKills = GetPlayerWithMostKills()
   print(PlayerWithMostKills)
   local Players = game:GetService("Players")   
   game:GetService('Players'):GetPlayers():WaitForChild('PlayerGui').MostKills.Enabled = true
 
local player = Players.LocalPlayer 
 

local userId = PlayerWithMostKills.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
 

local imageLabel = game.StarterGui.MostKills.Frame.PlrImage
imageLabel.Image = content
imageLabel.Size = UDim2.new(0, 166,0, 154)

This script is in serverscriptservice and also, when I don’t kill anyone, it prints “nil” and the script stops working.

There’s your issue, StarterGui’s descendants are only replicated on spawn (or only on join if ResetOnSpawn is false).

I’d recommend integrating a RemoteEvent where you call FireAllClients with the information (maybe as a dictionary) and a LocalScript, inside the UI, Connects upon OnClientEvent which applies the arrived information.

1 Like

Ok can you give me an example? (I’m not very experienced, and an example will really help!)

1 Like

Certainly, RemoteEvents are a valuable concept so make sure you understand the server/client boundary and Remotes firstly:

Anyway, for an example with a RemoteEvent inside ReplicatedStorage:

--// Script
game:GetService("ReplicatedStorage").RemoteEvent:FireAllClients({
    Content = content;
    Size = UDim2.new(0, 166, 0, 154);
});
--// LocalScript, inside UI
game:GetService("ReplicatedStorage").RemoteEvent.OnClientEvent:Connect(function(Information)
    --// Now do what you'd like with the dictionary
end)

Also, would you be able to show your ‘GetPlayerWithMostKills’ function? Seemingly, it’s returning nil.

1 Like

“content” would be the image label correct?

1 Like

Can you show your ‘GetPlayerWithMostKills’ function?

1 Like

There I forgot that part but I added it on (It’s at the top of the script)

1 Like

Yes of course.

Also, inside your ‘GetPlayerWithMostKills’ function I’d recommend defining current as -1 since if no-one has >0 kills then plr will stay nil. Make to assign current to the Player’s kills otherwise it’ll stay constant and pick any Player.

1 Like

now it says: ServerScriptService.GameManager:41: attempt to call a nil value (That’s the game:GetService(‘Players’):GetPlayers():WaitForChild(‘PlayerGui’).MostKills,Enabled = true part)

1 Like

GetPlayers() returns an array and you can’t use WaitForChild in an array.

1 Like

So you just do game:GetService(‘Players’):GetPlayers().PlayerGui.MostKills.Enabled = true?

1 Like

If that’s what you are trying to do.

1 Like

ok now it says this: ServerScriptService.GameManager:41: attempt to index nil with ‘MostKills’

1 Like

Is this a local script or a server script? If it’s a server script and you made the GUIs inside a local script, the server won’t see it.

1 Like

This is in a serverscript in serverscriptservice

1 Like

You need to iterate through the GetPlayers array in order to reach each Player’s PlayerGui. I endorse against doing this however; you should assign Enabled in the LocalScript.

1 Like

Yeah but I want everyone in the server to see this

1 Like

FireAllClients fires to every Player, therefore OnClientEvent will run for each and every one of them.

1 Like

So like this?

     '''
      --serverscript
      game.ReplicatedStorage.ShowImage:FireAllClients()
    
     --local script
     game.ReplicatedStorage.ShowImage.OnClientEvent:Connect(function()
    script.Parent.Enabled = true

end)

     '''
1 Like

Just like that! Remember to use GetService since it’s the most canonical way to retrieve services.

1 Like