Script working in studio not in-game

Hiya! Im making a player-list which includes the users profile, user and rank; however I’ve ran into a problem. My script works perfectly in studio but it does not work in-game. for the script my event has been fired through a server script which works fine.

here is my script for reference


local Parent = script.Parent
local ClickSound = Parent.Click
local Group = 15777400
local MinStaffRank = 100

local HttpService = game:GetService("HttpService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Event = ReplicatedStorage.Events.StaffPlayerList
local CommsFrame = Parent.CommsFrame
local StaffList = CommsFrame.PlayerList.StaffList
local PlayerTemplate = CommsFrame.PlayerList.PlayerTemplate

function OnPlayerJoined()
	Event.OnClientEvent:Connect(function(Player,Rank)
		for _,Staff in pairs(Players:GetChildren()) do
			local NewPlayer = PlayerTemplate:Clone()
			NewPlayer.Visible = true
			NewPlayer.User.Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..Staff.userId.."&width=420&height=420&format=png"
			NewPlayer.Username.Text = Staff.Name
			NewPlayer.Rank.Text = Rank
			NewPlayer.Parent = StaffList
		end
	end)
end

OnPlayerJoined()
while wait(.5) do
	OnPlayerJoined()
end

In-game the template just does not appear, I am not exactly sure of what is the problem.
Any help is appreciated

Well, I would say there are multiple issues with this. For one, you are constantly adding event connections in a while loop. For two, you solely rely on the server to run this when you can simply rely on the client for this. Lastly, you never remove any of the player’s from the list for an on leave.

local Parent = script.Parent
local ClickSound = Parent.Click
local Group = 15777400
local MinStaffRank = 100

local HttpService = game:GetService("HttpService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Event = ReplicatedStorage.Events.StaffPlayerList --dont really need this
local CommsFrame = Parent.CommsFrame
local StaffList = CommsFrame.PlayerList.StaffList
local PlayerTemplate = CommsFrame.PlayerList.PlayerTemplate

function LoadList()
	for i,v in pairs(StaffList:GetChildren()) do--not sure if there is other important stuff in it
		if v.Name == "PlayerTemplate" then
			v:Destroy()
		end
	end
	
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr:GetRankInGroup(Group) >= MinStaffRank then
			local NewPlayer = PlayerTemplate:Clone()
			NewPlayer.Visible = true
			NewPlayer.User.Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..plr.UserId.."&width=420&height=420&format=png"
			NewPlayer.Username.Text = plr.Name
			NewPlayer.Rank.Text = plr:GetRoleInGroup(GroupId)
			NewPlayer.Parent = StaffList
		end
	end
end

Players.PlayerAdded:Connect(LoadList)
Players.PlayerRemoving:Connect(LoadList)
1 Like

Why are you calling this function forever in a tight loop?

1 Like

Hiya! first off I would just like to say thank you so much, as soon as I read through this I realised I didn’t really need my server script, I’ve learnt quite alot from this.

However, upon testing this the template does not show up, no output either. any guidance would be good!

if I’m honest I thought it’d be easier, but I was clearly mistaken.

-- ... your initialisation definitions...

local tablePlayers = {};

local function makeTemplate(player)
	if player:GetRankInGroup(Group) >= MinStaffRank then
		local NewPlayerTemplate = PlayerTemplate:Clone();
		if not NewPlayerTemplate then return end;

		NewPlayerTemplate.Visible = true;
		NewPlayerTemplate.User.Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..plr.UserId.."&width=420&height=420&format=png";
		NewPlayerTemplate.Username.Text = player.Name;
		NewPlayerTemplate.Rank.Text = player:GetRoleInGroup(GroupId);
		NewPlayerTemplate.Parent = StaffList;
		
		-- index is player instance, value is player template
		tablePlayers[player] = NewPlayerTemplate;
	end
end

local function onPlayerAdded(player)
	-- Check if the player is in the list
	if tablePlayers[player] == nil then
		makeTemplate(player);
	end
end

local function onPlayerRemoved(player)
	-- Check if the player is in the list
	if tablePlayers[player] ~= nil then
		tablePlayers[player]:Destroy(); -- destroy template (value)
		tablePlayers[player] = nil; -- nil the index
	end
end

Players.PlayerAdded:Connect(onPlayerAdded);
Players.PlayerRemoving:Connect(onPlayerRemoved);
1 Like

Hiya! Thank you so much for taking the time to write this out, I really appreciate this!

when testing I am having the same outcome as the previous code, It just does not appear, nor output, any help is appreciated, if you are not sure of the cause its alright, I thank you anyway

I believe this is because either you joined the server after the other players, or because nobody else joins. Although, I did forget that on my part. Simply call the function right away as well.

local Parent = script.Parent
local ClickSound = Parent.Click
local Group = 15777400
local MinStaffRank = 100

local HttpService = game:GetService("HttpService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Event = ReplicatedStorage.Events.StaffPlayerList --dont really need this
local CommsFrame = Parent.CommsFrame
local StaffList = CommsFrame.PlayerList.StaffList
local PlayerTemplate = CommsFrame.PlayerList.PlayerTemplate

function LoadList()
	for i,v in pairs(StaffList:GetChildren()) do--not sure if there is other important stuff in it
		if v.Name == "PlayerTemplate" then
			v:Destroy()
		end
	end
	
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr:GetRankInGroup(Group) >= MinStaffRank then
			local NewPlayer = PlayerTemplate:Clone()
			NewPlayer.Visible = true
			NewPlayer.User.Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..plr.UserId.."&width=420&height=420&format=png"
			NewPlayer.Username.Text = plr.Name
			NewPlayer.Rank.Text = plr:GetRoleInGroup(GroupId)
			NewPlayer.Parent = StaffList
		end
	end
end

Players.PlayerAdded:Connect(LoadList)
Players.PlayerRemoving:Connect(LoadList)
LoadList()
1 Like

Hiya, thank you so much, you have fixed my problem, I thank you so much! I really appreciate you taking the time to write it out for me!

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