My enemy wont work

function joined(player)

local part1 = player.Character.Head
local part2 = script.Parent
while true do
	wait()
	local magnitude = (part1.HumanoidRootPart.Position - part2.Position).magnitude
	if magnitude < 50 then
		workspace.gj.Humanoid:MoveTo(part1)
		wait()
	end
end end

game.Players.PlayerAdded:Connect(joined)

This should work, but it doesnt recognize head. it says “attempt to index nil with Head”
PS this is a normal script.

You should probably wait for the character to load in, so maybe something similar to game.Players.CharacterAdded instead

1 Like

Hmm, ill test that give me a sec

Do

local char = player.Character or player.CharacterAdded:Wait()
local part1 = char.Head
...

What this does is check if the character exists and if it doesn’t, wait until it gets added. Then index the head, because the head should exist by now.

I’ll check that after this. Thanks for the responses tho!

Both of them turned out as "Characteradded is not a valid member of game.Players

oh wait I forgot to change the connect thing!

Wait no I didnt… I don’t know whats wrong with this
function joined(player)
local char = player.Character or player.CharacterAdded:Wait()
local part1 = char.Head
local part2 = script.Parent
while true do
wait()
local magnitude = (part1.HumanoidRootPart.Position - part2.Position).magnitude
if magnitude < 50 then
script.Parnet.Parent.Parent.Parent.gj.Humanoid:MoveTo(part1)
wait()
end
end end
game.Players.CharacterAdded:Connect(joined)

You have to correct the player thing to character so it would be like this
function CharAdded(char)
local head = char.Head
end
game.Players.CharacterAdded:Connect(CharAdded)

The issue here is you’re doing game.Players.CharacterAdded, you have to do game.Players.PlayerAdded:Connect(joined) (last line)

OH I FORGOT
CHARADDED ONLY WORKS ON A PLAYER
player.CharacterAdded:Connect(function()
end)

   function joined(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local part1 = char.Head
	local part2 = script.Parent
	while true do
		wait()
		local magnitude = (part1.HumanoidRootPart.Position - part2.Position).magnitude
		if magnitude < 50 then
			script.Parnet.Parent.Parent.Parent.gj.Humanoid:MoveTo(part1)
			wait()
		end
	end 
game.Players.PlayerAdded:Connect(joined)

I’ll try it thank you! I’ll share some pictures of what i’m making when im done.
Yet again, character added is not a valid member of players

But last line change to:

game.Players.PlayerAdded:Connect(joined)

You can’t get character without player.

workspace.gj.Humanoid:MoveTo(part1)

Is gj unintentional or an NPC?

NPC, also thank you it worked when I changed it!

As I promised, here’s “THE SENTINEL”
They run off of fire. They were created in the medieval times to fight off spirits, but eventually the king didnt know what to do with the spirits. So he forced them into “The Sentinels”, even if he knew that the sentinels are extremely strong, the sentinels rust in the rain, so the king hoped they would all rust away.

That’s really cool! Seems like you have an awesome game idea.

I’ll leave a link here when its done.