|
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!
|
|
amandaJoin Date: 2006-11-21 Post Count: 5925 |
_G.gameplayers is nil whenever your script reads it |
|
|
Right...how do I fix it? :) |
|
amandaJoin Date: 2006-11-21 Post Count: 5925 |
well
how about
what is _G.gameplayers
because your script says it doesn't exist or has no value |
|
|
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. |
|
|
Anyone else have any ideas? :) |
|
|
|
She never said replace it, she asked what the value _G.gameplayers is.
|
|
|
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
|
|
|
|
|
|
Bump. Anyone got some ideas? |
|
|
Anyone, please? :( This error is wrecking my game, not letting items be saved on the player's death/exit. |
|
|
|
|
|
AjastraJoin Date: 2017-08-01 Post Count: 1461 |
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.
|
|
AjastraJoin Date: 2017-08-01 Post Count: 1461 |
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
|
|
|
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) |
|
AjastraJoin Date: 2017-08-01 Post Count: 1461 |
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.
|
|
|
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. |
|
|
If there's a better way to go about this, I am absolutely open to ideas. :) |
|
AjastraJoin Date: 2017-08-01 Post Count: 1461 |
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.
|
|