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

Why is 'FindFirstChild' returns nil when the model is there and the name of the part is correct?

I am trying to make a script when a player enters the game, a part(Sphere) will be cloned and its CFrame will be changed.

game.Players.PlayerAdded:Connect(function(hi)
local ff = game.Workspace.FF:Clone()
ff.CanCollide = false
ff.Anchored = true
local char = workspace:FindFirstChild(hi)
local hum = hi:WaitForChild("HumanoidRootPart")
while true do
ff.CFrame = hum.CFrame
wait()
end
end)

I am aware of the fact that using while true do is not a very good idea but I am still a very amateur scripter :V . I tried this script both in localscript and script and is Parented by workspace. For script, it does give out an error - 'Attempting to set 'hum'(a nil value).Line 6' Something like that. But when I tried in localscript and it does not print out any errors nor does it work.

1 comment

0

hi isnt a part of workspace

1 answer

✓ Accepted answer

First of all, you have to unedrtstand that the PlayerAdded event's paramter, that you set as "hi", is not the player inside workspace, in fact that's not even a player. It's a character. You see, player has 2 thingies, a Player object, that you find in the Players section, and that is simply there to show all players in a game and to have local information about the player, such as its name and id... And a Character, which is the model that you find inside workspace, that is the physical form of the player. It's a model, containing all bodypart, the player's humanoid, and many other things, that represent the see-able side of the player. As I said, the PlayerAdded paramater is the player object of the player that joined firing that event, not the character. And you were searching for the character inside workspace, and that's wrong, since you are using "hi" which is the player inside Players. How to do this? Easy, you can do player.Character which refers to the player's character.

Another thing, :FindFirstChild() takes a string as an argument, and hi or hi.Character is an instance, so we would use tostring, which turns any type of value into a string, we would turn an instance to a string. (strings if you dont know are the characters inside "", those are string) this is a string "potato182".

game.Players.PlayerAdded:Connect(function(hi)
local ff = game.Workspace.FF:Clone()
ff.CanCollide = false
ff.Anchored = true
local char = 		workspace:WaitForChild(tostring(hi.Character))
local hum = char:WaitForChild("HumanoidRootPart")
while true do
ff.CFrame = hum.CFrame
wait()
end
end)

That's it!

3 comments

0

Thanks!!!

0

Np!

0

you could just .Name it instead of tostring() it but ok.

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