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
|
  |
| Joined: 04 Dec 2006 |
| Total Posts: 22 |
|
| |
|
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 |
|
|
|
| 16 Jul 2017 05:29 PM |
| change FindFirstChild to WaitForChild |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|