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

How to make a script find the next number up?

It's kinda hard for me to ask the question than to explain it. So I am working on a spectating gui and the way it works is the localscript only goes up by 1. In the replicated storage there is a folder named "PlayerList" which has ObjectValues, the names of the values are a number although when a player dies they are removed from the list. This causes gaps in the PlayerList. For instance there are 3 players. So there are 3 ObjectValues inside the PlayerList folder. Player 2 dies making the folder have 2 values named "1" and "3". Now when a player ties to go from "1" to "3" my script won't work because it will try to find "2" which is nil.

Script:

local bttn = script.Parent
local currentSpec = bttn.Parent.Spectating
local playerList = game.ReplicatedStorage.Game.PlayerList
local sounds = game.ReplicatedStorage.Sounds

bttn.MouseButton1Click:Connect(function() --Clicked
	
	if currentSpec.Value == playerList.Reserve.Value then
	else
		sounds.Click:Play()
		currentSpec.Value = currentSpec.Value + 1 --Heres the problem.
	end
	
end)

1 answer

I don't quite know how effective this will be.

Try This:

for i,v in pairs(playerlist:GetChildren()) do
	v.Name = i
end

Hope This Helped!

14 comments

0

what does this exactly do? im a little confused.

0

What it basically does is recount the items in playerlist, and relabel them. Lets say we have 1 and 3 in playerlist, this will rename it to 1 and 2. Since their are two items in playerlist

0

so is there something i should edit?

0

no, just plug it in before you add to the camspec value

0

im really trying to understand

0

sorry, put the code in before you add to current spec

0

bad argument #1 to 'pairs' (table expected, got Object)

0

woops, my bad. Re-Edited

0

the server would have to rename the values because if a player joins mid game it wouldnt change

0

Ok..then do that.

0

ok so "i" would be the value right?

0

No, i is the index

0

This is an unstable way to renumber things in Lua, because neither pairs nor GetChildren() guarantee that items will be handled in any particular order. So item 1 could be renumbered to 2, and 3 to 1. If you're OK with that, it will work. If you need to preserve relative order, you need to sort and manually compact an array.

0

Can you give any examples on the best way to do that?

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