|
| 14 Aug 2017 05:21 PM |
Hello! I keep getting an error with my Global Death Script.
The error I get is: -- Line 7 "bad argument #1 to 'pairs()' (table expected, got nil) "
I've marked the line where I get the error below:
game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:Connect(function(character) character:WaitForChild('Humanoid').Died:connect(function() local Update = {} for i,v in pairs(_G.gameplayers) do --ERROR LINE if v ~= player.Name then table.insert(Update, 1,v) end end _G.gameplayers = Update end) end) end)
Thoughts? Seems like it should be an easy fix, but I don't know what it is. :D Help is much appreciated. Thanks!
|
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 14 Aug 2017 05:46 PM |
| _G.gameplayers is nil whenever your script reads it |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2017 06:06 PM |
| Right...how do I fix it? :) |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 14 Aug 2017 06:06 PM |
well
how about
what is _G.gameplayers
because your script says it doesn't exist or has no value |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2017 06:30 PM |
Hello amanda!
Thanks for the response, but that doesn't quite work. Replacing the line:
for i,v in pairs(_G.gameplayers) do
with
_G.gameplayers
Does not solve the problems. I appreciate the Jeopardy answer humor, but please, if you have the solution, provide it in response. :) I wouldn't be posting if I had the answer myself. |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2017 07:29 PM |
| Anyone else have any ideas? :) |
|
|
| Report Abuse |
|
|
| |
|
|
| 14 Aug 2017 08:30 PM |
She never said replace it, she asked what the value _G.gameplayers is.
|
|
|
| Report Abuse |
|
|
|
| 14 Aug 2017 08:48 PM |
You're absolutely right. My apologies to Amanda. I immediately assumed negativity when there was none.
The _G.gameplayers is the players in the game, it interacts with my main script that calls all the players.
From the main script it looks:
-- Round System Setup
_G.gameplayers = {} for i, v in pairs(game.Players:GetPlayers()) do if v then table.insert(_G.gameplayers, v.Name) end end
--Intermission --Map Selection -- Round -- Repeat
|
|
|
| Report Abuse |
|
|
| |
|
| |
|
| |
|
|
| 15 Aug 2017 12:13 PM |
| Bump. Anyone got some ideas? |
|
|
| Report Abuse |
|
|
|
| 15 Aug 2017 02:37 PM |
| Anyone, please? :( This error is wrecking my game, not letting items be saved on the player's death/exit. |
|
|
| Report Abuse |
|
|
| |
|
| |
|
| |
|
| |
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 15 Aug 2017 10:41 PM |
Hi Never. _G.gameplayers is nil when you are attempting to iterate over it. Pairs iterates over a table.
_G.gameplayers is not defined when the code is executing. Additionally, I find that _G is hardly ever necessary and ModuleScripts are much better.
|
|
|
| Report Abuse |
|
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 15 Aug 2017 10:43 PM |
Also, as a matter of good practice and slight efficiency boost, you can improve your table access by converting its structure to a hash table rather than an array.
For example, if we want to check if a player is alive, we should do this:
local alivePlayers = { ["PlayerName"] = true }
print(alivePlayers["PlayerName"])
Rather than this:
local alivePlayers = { "PlayerName" }
for key, value in pairs(alivePlayers) do if value == "PlayerName" then print(value) end end
|
|
|
| Report Abuse |
|
|
|
| 15 Aug 2017 11:34 PM |
Ajastra - Thank you so much for your response!!
...I don't think I got the Global Death Script right, though... XD;;;
Help please?
game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:Connect(function(character) character:WaitForChild('Humanoid').Died:connect(function()
local alivePlayers = { ["PlayerName"] = true }
print(alivePlayers["PlayerName"])
end) end) end) |
|
|
| Report Abuse |
|
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 15 Aug 2017 11:43 PM |
That code would result in a syntax error; my reply was just to show an example of what you could do. What is the intended functionality you want from your script?
It appears in the original post you are redefining the _G.gameplayers table for some reason I'm not aware of.
|
|
|
| Report Abuse |
|
|
|
| 16 Aug 2017 12:06 AM |
| Its my Global Death Script, to let the game know people have died. To be honest, its a leftover script from the AlvinBlox Rounds system tutorial I originally used to set up the basis of my Rounds game, and then heavily modified the concept, but the base script was there. I needed a Global Death Script according to the tutorial in addition to a Global Leave Script. But at some point, it started giving me that error message, and I noticed that when the script doesn't work, items purchased by the player don't save either. |
|
|
| Report Abuse |
|
|
|
| 16 Aug 2017 12:07 AM |
| If there's a better way to go about this, I am absolutely open to ideas. :) |
|
|
| Report Abuse |
|
|
Ajastra
|
  |
| Joined: 01 Aug 2017 |
| Total Posts: 1461 |
|
|
| 16 Aug 2017 12:09 AM |
If you need to track what players are alive, there's no reason not to keep track of this in the main round script. Players leaving can also be tracked there as well.
You can even disconnect the events when a round isn't going on if you want to, by calling the RBXScriptSignal:Disconnect() method.
|
|
|
| Report Abuse |
|
|