I just want it to pick 1 person. I noted where I think the problem starts.
function chooseChosen() local gplrs = {} coroutine.resume(coroutine.create(function() local plrs = game.Players:GetChildren() for i = 1,#plrs do if CheckPlayers() then if 1 == 1 then print("There are ".. i.. " amount of players".." Game is starting") table.insert(gplrs,plrs[i]) local ran = math.random(1,#gplrs) -- Where I think the problem starts if gplrs[ran] ~= nil and gplrs[ran].Character ~= nil then local pran = gplrs[ran] for x = 1,1 do print(pran.Name) local Role = Instance.new("StringValue") Role.Name = "Role" Role.Value = "Chosen" Role.Parent = pran.Character if pran.Character:FindFirstChild("Role") then for c,v in pairs(pran.Character:GetChildren()) do if v.ClassName == "Part" then v.Transparency = v.Transparency + 1 end end end end end end end end end)) end
The main muddling-up I can see is to do with scoping. When you want to do something conditionally (with an if
statement) or when you want to loop over a number of things (with for
or while
) you have to include a 'full stop' or 'end of sentence' to signal that you're done with the looping bit or done with the conditional instruction, that's what the end
keyword is for.
You have put all of your end
statements at the end of the script, which means all of your loops and branches (if then else
checks) are nested - this is why tabbing/indenting is important.
Programmers use indentation to denote levels of nested control logic, like this:
for i=1,10 do if a then while c do for j=1,10 do -- do something end end end end
This makes it easier to see what is going on.
I'm not going to rewrite your code, because it seems awfully peculiar. Why is there a coroutine, what is if 1 == 1
for? What are all of those loops for? Etc...
Instead, I'll suggest a much better approach. Decompose what you want to achieve into separate, bitesize units of computation. You can call these mini-operations functions.
Suppose we have a table of players and we want to choose one of them at random. We could make a function that takes a table and chooses an object from it:
function ChooseRandomEntryFromTable(someTable) return someTable[math.random(#someTable)] --select random element end
Then, you can use this function in your own chooseChosen
to do something with the selected player:
function chooseChosen() plrs = game.Players:GetChildren() local chosenPlayer = ChooseRandomEntryFromTable(plrs) --do something with chosen player end
This doesn't do everything that I think you want to do, but it is a suggested structure which will make your code easier to understand, easier to write, and easier to edit. Separate the different operations that you want to perform into separate functions.
Please read this tutorial on scoping on the Official ROBLOX Wiki.