✓ Accepted answer
First, use the character added event to see if the head EXISTS.
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
if character.Head.Position.Z <= -100 then
game.ServerStorage.t2:Clone().Parent = game.Workspace
elseif character.Head.Position.Z > 100 then
game.Workspace.t2:Destroy()
end
end)
end)
What you can also use is the :DistanceFromCharacter() function. Instead of seeing if the z is -100 studs away.
local player = game:GetService("Players").LocalPlayer
player:GetDistanceFromCharacter(0,0,0)
So with that we can make your script better.
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
local dist = player:DistanceFromCharacter(0,0,0)
if dist < 100 then
game.ServerStorage.t2:Clone().Parent = game.Workspace
elseif dist > 100 then
game.Workspace.t2:Destroy()
end
end)
end)
But this only works once. After the character respawns. We can use a loop to get around them.
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
local dist = player:DistanceFromCharacter(0,0,0)
while wait() do
if dist < 100 then
game.ServerStorage.t2:Clone().Parent = game.Workspace
elseif dist > 100 then
game.Workspace.t2:Destroy()
end
end
end)
end)
Hope this helps!