Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't this normal script fire OnPlayerAdded?

local Players = game:GetService("Players")
function OnPlayerAdded(player)
	print (player.Name .. " has entered the game")

local Humanoid = game.Players.Parent:findFirstChild("Humanoid")
local torso = Humanoid.Parent.Torso


Players.PlayerAdded:connect(OnPlayerAdded)

-- etc.... The script continues but it's unnecessary information.




Also, i want to get the Humanoid from the Player, but i'm really struggling on how to get the correct syntax on finding the Humanoid.

But the main problem is the OnPlyaerAdded Event, it doesn't fire, it doesn't do the print.

3 answers

✓ Accepted answer

You're not checking if Player's Character have humanoid but instead you're checking if games has Humanoid.

local Players = game:GetService("Players")
function OnPlayerAdded(player)
    print (player.Name .. " has entered the game")

local Humanoid = player.Character:findFirstChild("Humanoid")
if Humanoid then--You aren't checking if Humanoid is actually there if it isn't your script will error. 
local torso = Humanoid.Parent.Torso
--Code
end
Players.PlayerAdded:connect(OnPlayerAdded)

~UserOnly20Charcters, Hoped I helped you to answer your question! If you have any further question, don't hesitate to comment below!!

1 comment

0

Thank you, was too blind to see that lol.

You should wait for the player's character to load, so you should add the line below under the print.

repeat wait() until player.Character This line checks if the player's character has loaded yet, if it has; it runs the rest of the code, if it hasn't; it will just keep repeating itself.

(Thanks to UserOnly20Characters for the original code below)

local Players = game:GetService("Players")

function OnPlayerAdded(player)
       print (player.Name .. " has entered the game")

	local Humanoid = player.Character:findFirstChild("Humanoid")
	if Humanoid then
		local torso = Humanoid.Parent.Torso
	end
end

Players.PlayerAdded:connect(OnPlayerAdded)

Reference: http://wiki.roblox.com/index.php?title=Loops#Repeat

2 comments

0

Thank you GullibleChapV2 for giving me the solution to this, pretty much can't think of that little details as im not such a pro in scripting. :)!

0

Other than a repeat loop, you can use the CharacterAdded function. http://wiki.roblox.com/index.php?title=API:Class/Player/CharacterAdded

~UserOnly20Charcters, it gives me this error:

  • Workspace.Script:5: attempt to index field 'Character' (a nil value)

Even setting it as player.Parent, so when someone joins it gets the parent of it so it identifies the character it still doesnt....

1 comment

0

You should read my answer below.

This discussion is preserved as it was. New answers, comments, and votes are closed.