|
| 24 Apr 2014 04:05 PM |
How do I make it so that it gets players who have something in their PlayerGui, but forgets those who don't have it? Here's what I have, not working:
local players = game.Players:GetChildren() for x = 1, #players do if players[x] ~= it then if players[x].PlayerGui.MapVote ~= nil then players[x].PlayerGui.MapVote:Destroy() elseif players[x].PlayerGui.MapVote == nil then --nothing end end end |
|
|
| Report Abuse |
|
|
|
| 24 Apr 2014 04:11 PM |
local players = game.Players:GetChildren() for x = 1, #players do end
^Dont use that. We have 'for' loops for tables.
local players = game.Players:GetChildren() for i, v in pairs(players) do --Dont worry about 'i', its not used much. 'v' is basically 'x' end |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 07:59 AM |
Yeah, it's not working. I think I did something wrong:
local players = game.Players:GetChildren() for i, v in pairs(players) do --Dont worry about 'i', its not used much. 'v' is basically 'x' if players[v].PlayerGui.MapVote ~= nil then players[v].PlayerGui.MapVote:Destroy() elseif players[v].PlayerGui.MapVote == nil then --nothing end end |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 08:16 AM |
| Error says that on this line: if players[v].PlayerGui.MapVote ~= nil then "Attempt to index field '?' a nil value" |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 26 Apr 2014 08:22 AM |
local tab = {"Hello World!","What a nice day!"}
for Index, Value in pairs(tab) do print(Index,Value) end
Run that to get the hang for these loops. |
|
|
| Report Abuse |
|
|
| |
|
|
| 26 Apr 2014 10:42 AM |
| if players[v].PlayerGui:FindFirstChild("MapVote") ~= nil then --Use :FindFirstChild() to check with nil |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
| |
|
|
| 26 Apr 2014 10:52 AM |
V is the value of the table, which in this case is the player, so just use v instead of players[v].
Here's my siggy... Done. |
|
|
| Report Abuse |
|
|
| |
|
|
| 26 Apr 2014 12:10 PM |
| I think the only way for me to learn right now is for the full script to be posted? If this happens, please try to explain each part. |
|
|
| Report Abuse |
|
|
| |
|
|
| 26 Apr 2014 12:55 PM |
you could try it like this also
for i,player in pairs(game.Players:GetChildren())do if player then if player.PlayerGui.MapVote then player.PlayerGui.MapVote:Destroy() else --nothing end end end |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 01:12 PM |
@OP Look at my script for something completely different.
parts = {game.Workspace.Part1, game.Workspace.Part2, game.Workspace.Part3} --Making a table, showing parts in Workspace. These are OBJECTS, NOT STRINGS.
for index,value in pairs(parts) do --Let's now loop through each value inside the table. Index is the position of the part in the table, value is the actual value in the position. value.BrickColor = BrickColor.new("Bright red") --See how I don't use parts[value]. This turns the current value's BrickColor (in this case, one of the objects) in the script has looped through, to Bright Red. end
Important facts to remember: When using :GetChildren(), the table returns objects, not strings. The first part of the for loop (index) is the position of the value the script has gone through in the loop. The second part of the for loop (value) is the actual value that the script has gone through in the loop. When trying to edit a value in a table, make sure you don't use parts[value] (could help in some cases, but not most of the time), just use value.
Here's my siggy... Done. |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 01:15 PM |
There's no reason to change his to a generic for loop, when that isn't even the problem here.
local players = game.Players:GetChildren() for x = 1, #players do if players[x] ~= it then if players[x].PlayerGui:FindFirstChild("MapVote") ~= nil then players[x].PlayerGui.MapVote:Destroy() else --nothing end end end |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 02:37 PM |
| Thank you Josh, yours worked. |
|
|
| Report Abuse |
|
|