Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't this normal script fire OnPlayerAdded?

Asked by 8 years ago
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

Log in to vote
1
Answered by 8 years ago

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!!

0
Thank you, was too blind to see that lol. iDarkGames 483 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

~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....

0
You should read my answer below. GullibleChapV2 155 — 8y
Log in to vote
0
Answered by 8 years ago

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

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. :)! iDarkGames 483 — 8y
0
Other than a repeat loop, you can use the CharacterAdded function. http://wiki.roblox.com/index.php?title=API:Class/Player/CharacterAdded GullibleChapV2 155 — 8y

Answer this question