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

(Chances) Players who shouldn't be selected, get selected. Any help?

I am making chances, similar to how MM2 made. This code is acting weird, like the selected player wont get selected, but the other player with no chances gets selected. And usually both players will get selected sometimes. Any ideas why this ain't working?

Code (in Script ServerScriptService):

local Chances = {}

while wait(8) do

repeat wait() until #game.Players:GetPlayers() >= 2
for _,player in pairs(game.Players:GetPlayers()) do
    for i = 1,math.max(1,player.Chance.Value) do
        table.insert(Chances,player.Name)
    end
end

local Choice

for i,v in pairs(game.Players:GetPlayers()) do
	Choice = Chances[math.random(#Chances)]
	if v.Name == Choice then
		v.Chance.Value = 1
		warn(v.Name,"Chances:",v.Chance.Value," SHOULD be selected")
	else
		v.Chance.Value = v.Chance.Value + 1
		warn(v.Name,"Chances:",v.Chance.Value," SHOULD NOT be selected")
	end
end

end

1 answer

I fixed some common issues. Never use :GetPlayers, Instead use :GetChildren. And don't use while wait() do as it will be very buggy. Don't use warn without a return. Print is a better method anyway. AND as well the script is fixed!

local Chances = {}

while true do
wait(8)
repeat wait() until #game.Players:GetChildren() >= 2
for _,player in pairs(game.Players:GetChildren()) do
    for i = 1,math.max(1,player.Chance.Value) do
        table.insert(Chances,player.Name)
    end
end

local Choice = false --This had to be at least a value!

for i,v in pairs(game.Players:GetChildren()) do
    Choice = Chances -- [math.random(#Chances) Why was this here? It picks the person above anyway.
    if v.Name == Choice then
        v.Chance.Value = 1
        print(v.Name,"Chances:",v.Chance.Value," SHOULD be selected")
    else
        v.Chance.Value = v.Chance.Value + 1
        print(v.Name,"Chances:",v.Chance.Value," SHOULD NOT be selected")
    end
end

4 comments

0

For what reason is it better to use :GetChildren() instead of :GetPlayers()? I personally use GetPlayers instead

0

GetPlayers() is better because it only gets players, -1 for false information

0

None of these changes make sense. While loop wih a wai in it, makes sense. But GetPlayers, I don't get at all. Plus I chose the math.random so it can choose a random player and other players have chances if that makes sense.

0

You don't even need a second loop. You can literally set Choice to be a random player without the second loop, then print the player's name in the given print() function.

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