Scripting mob to respawn

local Humanoid = script.Parent
local HumanoidRootPart = script.Parent.Parent.HumanoidRootPart

local function Respawn()
	if Humanoid.Health == 0 then
		wait(15)
		print("Respawning")
		script.Parent.Parent.Crystal.Transparency = 0
		Humanoid.Health = 100
		Humanoid.MaxHealth = 100
	end
end

local function Death()
	if Humanoid.Health == 100 then return end
	if Humanoid.Health <= 0 then
		spawn(function()
			print("Died")
			script.Parent.Parent.Crystal.Transparency = 1
			
			local Shatter = script.Shatter:Clone()
			Shatter.Parent = HumanoidRootPart
			Shatter.Pitch = 1.15 * (math.random(90, 110) / 100)
			Shatter:Play()
			game.Debris:AddItem(Shatter,1.5)
			--wait(15)
			--print("Should be showing")
			--script.Parent.Parent.Crystal.Transparency = 0
			--Humanoid.Health = 100
			--Humanoid.MaxHealth = 100
		end)

	end
end


local function Hit()
	if Humanoid.Health == 0 then return end
	if Humanoid.Health > 0 then
		print("Still ALive")
		local Glass = script.Glass:Clone()
		Glass.Parent = HumanoidRootPart
		Glass.Pitch = 1.15 * (math.random(90, 110) / 100)
		Glass:Play()
		game.Debris:AddItem(Glass,1.5)
	end
end

Humanoid.HealthChanged:connect(Hit)
Humanoid.HealthChanged:connect(Death)
Humanoid.HealthChanged:connect(Respawn)

Is there a better way to script this? I’m also using a serverscript I want to use a local script.

1 Like

I recommend this:

Uhh… A local script wont work… if you made the mob to be completely local sided, (and you spawn multiple,) that may cause lag on the users end if the user doesn’t have high cpu.

If you REALLY want it local sided, move the mob into game>starterplayer>starterplayerscripts, and make the scripts local sided. Depending in what other scripts you have, you may need to do more.

Or you may also need to edit the script too idk atm lmk if it doesn’t work and ill look more at the script.

1 Like

Thank you I didn’t know it would make the player lag.