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: Why does this print out nil?

Previous Thread :: Next Thread 
Surgo is not online. Surgo
Joined: 11 Jan 2011
Total Posts: 2748
16 Jul 2017 05:22 PM
game.Players.PlayerAdded:connect(function(newPlayer)
local character = game.Workspace:FindFirstChild(newPlayer.Name)
print(newPlayer.Name) --This will print out characters name
local newscript = script.OnDeath:Clone()
newscript.Parent = character
newscript.Disabled = false
print(newscript.Parent) --this prints out nil?????
end)
Report Abuse
DarkLight is not online. DarkLight
Joined: 04 Dec 2006
Total Posts: 22
16 Jul 2017 05:23 PM
because character is nil
Report Abuse
6l8 is not online. 6l8
Joined: 17 Jun 2014
Total Posts: 4055
16 Jul 2017 05:25 PM
When someone joins, their character isn't loaded right away and takes some time.


Report Abuse
systematicaddict is not online. systematicaddict
Joined: 13 Jul 2012
Total Posts: 4188
16 Jul 2017 05:29 PM
change FindFirstChild to WaitForChild
Report Abuse
TrainingToTroll is not online. TrainingToTroll
Joined: 27 Oct 2012
Total Posts: 1757
16 Jul 2017 05:32 PM
game.Players.PlayerAdded:connect(function(newPlayer)
repeat wait() until newPlayer.Character~=nil
local character = game.Workspace:FindFirstChild(newPlayer.Name)
print(newPlayer.Name) --This will print out characters name
local newscript = script.OnDeath:Clone()
newscript.Parent = character
newscript.Disabled = false
print(newscript.Parent) --this prints out nil?????
end)
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
16 Jul 2017 06:25 PM
"repeat wait() until newPlayer.Character~=nil
local character = game.Workspace:FindFirstChild(newPlayer.Name)"

You're doing it kind of correctly at first but then you're looking for the character in Workspace for no reason. What if the player has a generic username and something else in Workspace conflicts with it?

This is all you need:

game:GetService("Players").PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
end)

`character` will be a reference to the player's character if it exists during assignment. Otherwise, the Wait method will yield until CharacterAdded is invoked and will return any arguments that it would pass to a function like normal.


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