Problem with custom team character script

local zombieteam = game.Teams.Zombies
local players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local zombiespawnpoint = workspace:FindFirstChild("z team spawn")

local mutations = {
	{name = "Normal", chance = 85},
	{name = "Haunted", chance = 56.55},
	{name = "Brute", chance = 31.2010404325567653423456},
	{name = "Spitter", chance = 45.445},
	{name = "Bomber", chance = 30.45},
	{name = "Forsaken", chance = 65.68}
}

local spawntime = 5
local bruteCount = 0
local spawnFlags = {}

local function getRandomMutation()
	local totalWeight = 0
	for _, mutation in ipairs(mutations) do
		if mutation.name == "Brute" and bruteCount > 0 then
			continue
		end
		totalWeight = totalWeight + mutation.chance
	end

	local randomWeight = math.random() * totalWeight
	local cumulativeWeight = 0

	for _, mutation in ipairs(mutations) do
		if mutation.name == "Brute" and bruteCount > 0 then
			continue
		end
		cumulativeWeight = cumulativeWeight + mutation.chance
		if randomWeight <= cumulativeWeight then
			return mutation.name
		end
	end
end

local function handleBruteDeath(newZombie)
	local humanoid = newZombie:WaitForChild("Humanoid")
	if newZombie:GetAttribute("brute") then
		bruteCount = bruteCount + 1
	end
	humanoid.Died:Once(function()
		if newZombie:GetAttribute("brute") then
			bruteCount = bruteCount - 1
		end
	end)
end

local function spawnZombieWithAccessories(player)
	local mutationName = getRandomMutation()
	local newZombie = ServerStorage.Mutations[mutationName]:Clone()
	newZombie.Name = player.Name
	player.Character = newZombie
	for _, script in pairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
		local clonedScript = script:Clone()
		clonedScript.Parent = newZombie
	end
	newZombie:SetPrimaryPartCFrame(zombiespawnpoint.CFrame + Vector3.new(0, 2, 0))
	newZombie.Parent = workspace

	handleBruteDeath(newZombie)

	local toolToEquip
	if newZombie:GetAttribute("normalz") then
		toolToEquip = ServerStorage.ZombieTools:FindFirstChild("fist"):Clone()
	elseif newZombie:GetAttribute("ghost") then
		toolToEquip = ServerStorage.ZombieTools:FindFirstChild("haunt"):Clone()
	elseif newZombie:GetAttribute("brute") then
		toolToEquip = ServerStorage.ZombieTools:FindFirstChild("crush"):Clone()
	elseif newZombie:GetAttribute("toxic") then
		local spit = ServerStorage.ZombieTools:FindFirstChild("spit"):Clone()
		spit.Parent = player.Backpack
		toolToEquip = ServerStorage.ZombieTools:FindFirstChild("rotten"):Clone()
	elseif newZombie:GetAttribute("boomer") then
		toolToEquip = ServerStorage.ZombieTools:FindFirstChild("burst"):Clone()
	end
	if toolToEquip then
		toolToEquip.Parent = player.Backpack
	end
end

local function spawning(player)
	if spawnFlags[player.UserId] then return end  
	spawnFlags[player.UserId] = true

	if player.Team == zombieteam then
		task.wait(0.1)  
		if player.Team == zombieteam then
			spawnZombieWithAccessories(player)
		end
	else
		player:LoadCharacter()
		local normalAnimate = script.Animate:Clone()
		if not player.Character:FindFirstChild("Animate") then
			normalAnimate.Parent = player.Character
		end
	end

	task.delay(1, function()
		spawnFlags[player.UserId] = false
	end)
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Once(function()
			task.wait(spawntime)
			spawning(player)
		end)
	end)

	player:GetPropertyChangedSignal("Team"):Connect(function()
		spawning(player)
	end)

	spawning(player)
end

players.PlayerAdded:Connect(onPlayerAdded)

for _, player in pairs(players:GetPlayers()) do
	onPlayerAdded(player)
end

players.PlayerRemoving:Connect(function(player)
	if player.Team == zombieteam then
		local zombie = workspace:FindFirstChild(player.Name)
		if zombie then
			zombie:Destroy()  
		end
	end
end)

The player’s character is only changes when the player died, but if they somehow reset their character thats not dying then the character will become their avatar and not a mutation even they are in the zombie team.

I’d suggest moving this topic over to #help-and-feedback:scripting-support

1 Like