Having trouble with this player attack script

I run a function so whenever the player is added it runs, But when they die and respawn the function does not run again unless I die. is there any way to have it run and get the player over and over again regardless of if he dies or not?

code:


Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		NPC.PlayerAttack(Char)
	end)
end)


Hm? The .CharacterAdded code will run every time the player respawns.

From what I can see is that the npc only starts to attack when the player respawns, not when the npc respawns.

Yeah thats the problem. I want it to always find the closest player regardless of that.

It follows the player based off of HumRoot position. Can i get the closest char without that function?

What are you talking about with “closest player” or anything? The only thing you sent us is a script with a .PlayerAdded and .CharacterAdded.

Sorry about that

Code:

local NPC = {}


function NPC.PlayerAttack(Char, Self)
	local Players = game.Players
	local Fire = require(game.ServerScriptService.Shard)
	local Debounce = false
	local HumanoidRoot = Char:FindFirstChild("HumanoidRootPart")
	local Mag = (Self.HumanoidRootPart.Position - HumanoidRoot.Position).Magnitude
	if Mag < 15 and not Debounce then
		Debounce = true
		Fire.Make(Self.HumanoidRootPart)
		wait(2)
		Debounce = false
	else
		Self.Humanoid:MoveTo(HumanoidRoot.Position)
	end
end

return NPC


Full first script

local Players = game.Players
local Store = script.Parent:Clone()
local NPC = require(game.ServerScriptService.NPC)
local Debounce  = false
local Char 
print("Actiaved")

local function Look(Character)
	print("Looking")
	while wait() do
		if not Debounce then
			Debounce = true 
			NPC.PlayerAttack(Character, script.Parent)
			wait(1)
			Debounce = false
		end
	end
end



Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		Look(Char)
	end)
end)





1 Like

Oh. Guess no one has an answer…

1 Like