Attempt to index nil with waitforchild

I am making a soccer game and the issue I am having has to do with shooting. So the way it works is when the player shoots, if it goes into the goal, the ball is destroyed and a new one from server storage is cloned and placed in the center of the field for restart. What is annoying is it will work like 3 or 4 times in a row then all of the sudden there is an error that says attempt to index nil with waitforchild. This is the script for giving a player possession of the ball and the error always happens as soon as the new ball spawns in. The error is on line local possession1 = player:WaitForChild"Possession"

ball.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and Possession.Value == false then
		local char = hit.Parent
		local Players = game:GetService("Players")
		local player = Players:GetPlayerFromCharacter(char)
		local possession1 = player:WaitForChild"Possession"

It seems you made a simple typo on

local possession1 = player:WaitForChild"Possession"

should be

local possesion1 = player:WaitForChild("Possesion")

since WaitForChild is a function it needs to have brackets on the end of it with the name of the child between them

I added brackets and tried findfirstchild and now I am getting attempt to index nil with findfirstchild

Question do you have an object under player that is called Possesion?

1 Like

You can’t call Player service in a ServerScript.
Edit: You can call player service in a ServerScript, you just can’t get a player. Correction*

3 Likes

It is a bool value called possession

Well it works 3 or 4 times before I get the error

What @HitCraftPizzeria said is right if it’s a server script it cant reference the Players service

No, I’m pretty sure server scripts can access the Players Service. What they can’t access is the current client.
Ex:

game.Players.PlayerAdded:Connect(function(player)
    print(player.Name)
end)

That code, when placed in a server script, would print the name of a player who joins the game.

2 Likes

Actually try

ball.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) ~= nil and Possession.Value == false then
local char = hit.Parent
local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local possession1 = player:WaitForChild(“Possession”)

and also, did you define the other variables like Possession?

Yes the other variables have been defined and the issue with your script is that everything is in a server script and im pretty sure you cant call local player in a server script.

1 Like

How does the Possession on the same line as the hit.Parent:FindFirstChild(“Humanoid”) work. Who’s possession is it checking?

It can be touched by an NPC and since an NPC is not a player, it gives nil

ball.Touched:Connect(function(hit)
    local char = hit.Parent
    local player = game:GetService("Players"):GetPlayerFromCharacter(char)
    if player and Possession.Value == false then
    	local possession1 = player:WaitForChild"Possession"
1 Like

wait, did you make any folder to store the Possession value in??

Very dumb mistake from me I had a goalie in the goal that is an NPC and he was touching it every now and then, thanks to everyone trying to help.

1 Like