Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a script find the next number up?

Asked by 4 years ago

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

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago
Edited 4 years ago

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!

0
what does this exactly do? im a little confused. ReallyUnikatni 68 — 4y
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 Farsalis 369 — 4y
0
so is there something i should edit? ReallyUnikatni 68 — 4y
0
no, just plug it in before you add to the camspec value Farsalis 369 — 4y
View all comments (10 more)
0
im really trying to understand ReallyUnikatni 68 — 4y
0
sorry, put the code in before you add to current spec Farsalis 369 — 4y
0
bad argument #1 to 'pairs' (table expected, got Object) ReallyUnikatni 68 — 4y
0
woops, my bad. Re-Edited Farsalis 369 — 4y
0
the server would have to rename the values because if a player joins mid game it wouldnt change ReallyUnikatni 68 — 4y
0
Ok..then do that. Farsalis 369 — 4y
0
ok so "i" would be the value right? ReallyUnikatni 68 — 4y
0
No, i is the index Farsalis 369 — 4y
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. EmilyBendsSpace 1025 — 4y
0
Can you give any examples on the best way to do that? Farsalis 369 — 4y
Ad

Answer this question