What's a slower way to run code? I'm currently using RunService.Heartbeat, but this is too quick

I’ve been trying to figure out an alternative to this, however, so far I’ve come empty handed. I did try to make the code run each time a player joins, but, the custom leaderboard I’m using broke, as shown below
dd894511a50fc5ff92d2e835a9ed8565acd8e6e8

Here’s the code that works, but is wayy too fast and is causing lag.

RunService.Heartbeat:Connect(UpdatePlayerList)
RunService.Heartbeat:Connect(IconsManager.UpdatePlayerIcon)
3 Likes

if you’re fine with using a loop you could easily do this:

while true do task.wait(1)
    UpdatePlayerList()
    IconsManager.UpdatePlayerIcon()
end

But only running the functions when a player joins/leaves would be the best way if you can figure out how to.

1 Like

This fixed the constant loop, but now, the custom leaderboard ended up breaking, with error of

Science is not a valid member of ScrollingFrame "Players.bIIocky.PlayerGui.PlayerList.PlayerList
1 Like

Can you send the section of code that error is coming from?

1 Like

Sure!

	for _, team in pairs(game.Teams:GetChildren()) do
		if ServiceManager.PlayerListVisible == true then
			if Settings.teams.DisplayingAllTeams then
				PlayerList[team.Name].Visible = true
			else
				if #team:GetPlayers() == 0 then
					PlayerList[team.Name].Visible = false
				else
					PlayerList[team.Name].Visible = true
				end
			end
		else
			return
		end
	end
end

The 296th line is this line of code here

PlayerList[team.Name].Visible = false
1 Like

You should probably use PlayerList:FindFirstChild(team.Name) instead of trying to index it and also making sure that it could find the child. But I’m assuming the issue is happening because the teams on the player list aren’t generating fast enough. You need a way to wait until all the teams are inside the player list before looping

1 Like

Don’t link this to an iteration, instead link it to an event. Using an infinite loop will cause a lot of memory strain. A good way would be listening for a player join and then their team change, remember to disconnect connections when they leave though. If you made your own version of RBXScriptSignal, you could use your own custom signals.

1 Like

That’s what Signal is for

1 Like

I’m now facing a new errror of attempt to index nil with visible, and I presume its because the script is unable to find the team…

	for _, team in pairs(game.Teams:GetChildren()) do
		if ServiceManager.PlayerListVisible == true then
			if Settings.teams.DisplayingAllTeams then
				PlayerList.FindFirstChild(team.Name).Visible = true
			else
				if #team:GetPlayers() == 0 then
					PlayerList:FindFirstChild(team.Name).Visible = false
				else
					PlayerList:FindFirstChild(team.Name).Visible = true
1 Like

Apologies for my general lack of knowledge, I’m new to scripting,. and so I don’t know the proper terminology yet!

1 Like

Find first child returns nil if it can’t find the child, so you need to do an if statement to check if the child exists, and if it doesn’t you can just continue the loop. But that’s only going to solve the error, not the issue

1 Like

Another option is to manually code a fixed rate of your choosing:

local rate = 0.5 -- Ticks occur every x real seconds
local elapsed = 0

RunService.Heartbeat:Connect(function(dt)
	elapsed += dt
	while elapsed > rate do -- Do as many ticks needed to match real time
		elapsed -= rate
		-- TODO your code here
	end
end)
2 Likes

you’re right, I tried using an if statement, and now the error has disappeared, but now an unknown error has been called
image

1 Like

This works, however, there’s a new error which has appeared

Players.bIIocky.PlayerGui.PlayerList.Core.Modules.IconsManager:46: attempt to index nil with 'icon

Unsure why this is, but the rest of the script works so that’s cool!

1 Like

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