Unable to get player using Players:FindFirstChild(....)

Hey! I am trying to create a survivors tab which uses the players id to create a photo and username ui chip after a round is finished, but in my I am unable to get the player using Players:FindFirstChild() with a string value.

image

I have tried using task.wait, teams as well as looking for similar problems on the dev forum, but I am unable to find anything that relates to my problem and fixes it.

Any help would be appreciated I am quite new to scripting lua!

local events = game:GetService("ReplicatedStorage").Events
local player = game:GetService("Players").LocalPlayer
local roundInfo = game.ReplicatedStorage:WaitForChild("RoundInfo")
local Players = game:GetService("Players")

local gui = script.Parent
local ui = player.PlayerGui.UI_Elements.MainMenu.UI



events:WaitForChild("toggleMainMenu").OnClientEvent:Connect(function(visible)
	ui.Visible = visible
	print(visible)
	events.ShimeStop:Fire(true)
	
end)

events.Survivors.OnClientEvent:Connect(function(Survivors)

	print("rec")
	events.ShimeStop:Fire(false)
	print(Survivors)
	for i,v in ipairs(Survivors) do
		print(i, v)
		task.wait(0.1)
		local playerv = Players:FindFirstChild(v)
		-- Add a small delay to allow player object to replicate
		task.wait(0.1)

	
		-- Proceed with player object operations
		local playerImg = ui.MainUI.SurvivorsContainer.Survivors.PlayerTemplate:Clone()
		playerImg.ImageLabel.Shine.Enabled = true
		local img = game.Players:GetUserThumbnailAsync(playerv.UserId, Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size100x100)
		playerImg:WaitForChild("ImageLabel").Image = img
		playerImg.TextLabel.Text = playerv.Name
		playerImg.Name = player.Name
		playerImg.Visible = true
		playerImg.Parent = ui.MainUI.SurvivorsContainer.Survivors
	
		
		
		


	end

end)
2 Likes

FindFirstChild can return nil if it doesn’t find anything, I suggest using WaitForTime with a time-out duration of 5 seconds instead:

local events = game:GetService("ReplicatedStorage").Events
local player = game:GetService("Players").LocalPlayer
local roundInfo = game.ReplicatedStorage:WaitForChild("RoundInfo")
local Players = game:GetService("Players")

local gui = script.Parent
local ui = player.PlayerGui.UI_Elements.MainMenu.UI



events:WaitForChild("toggleMainMenu").OnClientEvent:Connect(function(visible)
	ui.Visible = visible
	print(visible)
	events.ShimeStop:Fire(true)

end)

events.Survivors.OnClientEvent:Connect(function(Survivors)

	print("rec")
	events.ShimeStop:Fire(false)
	print(Survivors)
	for i,v in ipairs(Survivors) do
		print(i, v)

		local playerv = Players:WaitForChild(v, 5) -- Will wait for 5 seconds for the player to load

		if playerv then -- If 5 second passes and no player is found, playerv will be nil so an if statement is needed
			-- Proceed with player object operations
			local playerImg = ui.MainUI.SurvivorsContainer.Survivors.PlayerTemplate:Clone()
			playerImg.ImageLabel.Shine.Enabled = true
			local img = game.Players:GetUserThumbnailAsync(playerv.UserId, Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size100x100)
			playerImg:WaitForChild("ImageLabel").Image = img
			playerImg.TextLabel.Text = playerv.Name
			playerImg.Name = player.Name
			playerImg.Visible = true
			playerImg.Parent = ui.MainUI.SurvivorsContainer.Survivors
		end

	end

end)
1 Like

I assume this doesn’t work because server sends survivors as an array of players instead of array of player names. You can check that by printing typeof(v) inside the for loop. If it prints Instance then you can remove playerv variable and replace its occurences with just v, because it would be the player instance

2 Likes

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