WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 05:05 AM |
game.Players.CharacterAdded(function(p)
How do I connect this to a PlayerAdded event? |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 05:07 AM |
I keep on making simple mistakes...
game.Players.CharacterAdded:connect(function(p)
There. Now how do I connect this line to a PlayerAdded event? |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 05:09 AM |
Like so,
game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(char)
-- script
end) end) |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 05:09 AM |
game.Players.PlayerAdded:connect(function(ply) ply.CharacterAdded:connect(function()
end) end) |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 05:10 AM |
CharacterAdded is actually a member of the Player, which you can get from the PlayerAdded event. The PlayerAdded event, in turn, is a member of game.Players. Here's a short example that I hope you can learn a bit from:
game.Players.PlayerAdded:connect(function(Plr) -- when a Player enters, Plr = the Player Plr.CharacterAdded:connect(function(Char) -- when the player spawns/respawns, Char = Character print(Plr, "respawned") -- print that the player respawned end) -- for the characteradded part end) -- for the playeradded part
~Sorcus |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 05:23 AM |
Thanks for the help, guys. I get it now.
Another question:
Can I do something like this?
game.Players.PlayerAdded:connect(function(Plr) --do stuff when the Player enters Plr.CharacterAdded:connect(function(Char) --do different stuff when the Character respawns end) end)t |
|
|
| Report Abuse |
|
|
| |
|
|
| 31 Jul 2011 05:25 AM |
| Yes. But without the invalid 't' at the end :p |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 05:28 AM |
Okay. Question though. With this script, the Wipeouts value starts at 1. Is this because the CharacterAdded portion runs when the Player is added?
game.Players.PlayerAdded:connect(function(Plr) local stats = Instance.new("IntValue") stats.Name = "leaderstats" stats.Parent = Plr local wos = Instance.new("IntValue") wos.Name = "Wipeouts" wos.Value = 0 wos.Parent = stats Plr.CharacterAdded:connect(function(Char) Plr.leaderstats.Wipeouts.Value = Plr.leaderstats.Wipeouts.Value + 1 end) end) |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 05:29 AM |
game.Players.PlayerAdded:connect(function(Plr) local stats = Instance.new("IntValue") stats.Name = "leaderstats" stats.Parent = Plr local wos = Instance.new("IntValue") wos.Name = "Wipeouts" wos.Value = -1 wos.Parent = stats Plr.CharacterAdded:connect(function(Char) Plr.leaderstats.Wipeouts.Value = Plr.leaderstats.Wipeouts.Value + 1 end) end) |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 05:38 AM |
Thanks. Now, will something like this be possible?
game.Players.PlayerAdded:connect(function(Plr) Plr.CharacterAdded:connect(function(Char) Char.Died:connect(function() end) end) end)
|
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 05:45 AM |
game.Players.PlayerAdded:connect(function(Plr) Plr.CharacterAdded:connect(function(Char) Char.Humanoid.Died:connect(function()
end) end) end)
Died is an event of humanoid, not character. |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 05:53 AM |
Let's say I remove the 2nd anonymous function line. How would I make something like this work?
game.Players.PlayerAdded:connect(function(Plr) Plr.Character.Humanoid.Died:connect(function() print("Character") end) end)
Character is a nil value according to the output error. |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 06:02 AM |
The character isn't added at the same time as the Player, it spawns for the first time after several seconds. You can use a repeat loop to wait for the character to exist:
repeat wait() until Plr.Character
Or you can use the Wait method of events, in this case the CharacterAdded event:
Plr.CharacterAdded:Wait()
That will wait until a character spawns.
~Sorcus |
|
|
| Report Abuse |
|
|
coljetix
|
  |
| Joined: 06 Nov 2007 |
| Total Posts: 100 |
|
|
| 31 Jul 2011 06:05 AM |
game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(char) char.Character.Humanoid.Died:connect(function()
--do on death
end) end) end) |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 06:07 AM |
Okay, I understand the repeat loop but I don't understand the wait() method.
What does the wait() method do?
The output error for this:
game.Players.PlayerAdded:connect(function(Plr) Plr.CharacterAdded:Wait() Plr.Character.Humanoid.Died:connect(function() print("Character") end) end)
is Line 2: Wait is not a valid member |
|
|
| Report Abuse |
|
|
coljetix
|
  |
| Joined: 06 Nov 2007 |
| Total Posts: 100 |
|
|
| 31 Jul 2011 06:09 AM |
this is invalid wait() function waits the least possible time until next line execution
he meant that on played added, you could do loop that checks if character still exists
game.Players.PlayerAdded:connect(function(Plr) while Plr.Character==nil then wait() end Plr.Character.Humanoid.Died:connect(function() print("Character") end) end)
|
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 06:09 AM |
Maybe ROBLOX is just stupid, and will only accept :wait().
Plr.CharacterAdded:wait()
Honestly, ROBLOX is making us capitalize almost everything, yet it doesn't allow us to capitalize other things. SO ANNOYING.
~Sorcus |
|
|
| Report Abuse |
|
|
coljetix
|
  |
| Joined: 06 Nov 2007 |
| Total Posts: 100 |
|
|
| 31 Jul 2011 06:10 AM |
| WishNite, you should re-read wiki.roblox.com scripting section, because it seems you lack some understanding of lua functions |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 06:11 AM |
@coljetix: I suggest you do the same.
"while Plr.Character==nil then wait() end"
~Sorcus |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2011 06:13 AM |
I know what wait(time) does. If it's wait(), then it waits a frame.
But what I don't get is this line:
Plr.CharactedAdded:wait() --What does the wait do? Does it wait until the Character is added? Is it just a shorter version of the repeat loop? |
|
|
| Report Abuse |
|
|
coljetix
|
  |
| Joined: 06 Nov 2007 |
| Total Posts: 100 |
|
|
| 31 Jul 2011 06:15 AM |
| it does not exist..i think, wait ill check maybe they added while i was gone for half a year |
|
|
| Report Abuse |
|
|
coljetix
|
  |
| Joined: 06 Nov 2007 |
| Total Posts: 100 |
|
|
| 31 Jul 2011 06:16 AM |
| nope Player has no :wait() |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2011 06:17 AM |
The wait method of events pauses the thread until the event has fired. Pausing the thread is a fancy way of saying making the script wait, and it will just keep waiting until the event fires (when a character is added).
It uses a loop, just like the repeat loop example, but it's coded in C++, so it's more efficient.
~Sorcus |
|
|
| Report Abuse |
|
|
coljetix
|
  |
| Joined: 06 Nov 2007 |
| Total Posts: 100 |
|
|
| 31 Jul 2011 06:17 AM |
| you should check object browser in roblox studio under Help/Object Browser |
|
|
| Report Abuse |
|
|