generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Global Death Script error

Previous Thread :: Next Thread 
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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 is not online. 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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
14 Aug 2017 06:06 PM
Right...how do I fix it? :)
Report Abuse
amanda is not online. 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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
14 Aug 2017 07:29 PM
Anyone else have any ideas? :)
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
14 Aug 2017 08:28 PM
Bump?
Report Abuse
dude10000009 is not online. dude10000009
Joined: 23 Nov 2010
Total Posts: 809
14 Aug 2017 08:30 PM
She never said replace it, she asked what the value _G.gameplayers is.


Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
14 Aug 2017 09:55 PM
Bump.
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
14 Aug 2017 10:54 PM
Bump. Anyone?
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
15 Aug 2017 09:47 AM
Bump
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
15 Aug 2017 12:13 PM
Bump. Anyone got some ideas?
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
15 Aug 2017 03:34 PM
Bump.
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
15 Aug 2017 07:36 PM
Bump.
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
15 Aug 2017 08:51 PM
Bump.
Report Abuse
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
15 Aug 2017 10:39 PM
Bump.
Report Abuse
Ajastra is not online. 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 is not online. 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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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 is not online. 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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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
Never2Humble is not online. Never2Humble
Joined: 04 Dec 2016
Total Posts: 147
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image