Hello again.
I have a zombies changer script which will be active when the player assigned into the “zombie” team, it works fine when player join the team that are not the zombie team at first, but when they joined the game with the zombie team assigned the script would spawn the player as their normal avatar.
Here is the script
local zombieteam = game.Teams.Zombies
local players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local zombiespawnpoint = workspace:FindFirstChild("z team spawn")
local chances = game.ReplicatedStorage.Chances
local mutations = {
{name = "Normal", chance = 73.4},
{name = "Haunted", chance = 56.55},
{name = "Brute", chance = 31.2},
{name = "Spitter", chance = 53.45},
{name = "Bomber", chance = 72.45},
{name = "Crawler", chance = 61.37},
{name = "Charger", chance = 59.98}
}
local spawntime = 5
local function getRandomMutation()
local totalWeight = 0
local cumulativeWeights = {}
for _, mutation in ipairs(mutations) do
totalWeight += mutation.chance
table.insert(cumulativeWeights, {name = mutation.name, weight = totalWeight})
end
local randomWeight = math.random() * totalWeight
for _, entry in ipairs(cumulativeWeights) do
if randomWeight <= entry.weight then
return entry.name
end
end
end
local function spawnZombieWithAccessories(player)
local mutationName = getRandomMutation()
local newZombie = ServerStorage.Mutations:FindFirstChild(mutationName):Clone()
if not newZombie then
warn("Mutation not found: " .. mutationName)
return
end
newZombie.Name = player.Name
player.Character = newZombie
for _, script in pairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
script:Clone().Parent = newZombie
end
newZombie:SetPrimaryPartCFrame(zombiespawnpoint.CFrame + Vector3.new(0, 2, 0))
newZombie.Parent = workspace
local toolMapping = {
Normal = {"fist"},
Haunted = {"haunt"},
Brute = {"crush"},
Spitter = {"spit", "rotten"},
Bomber = {"burst"},
Crawler = {"leap", "fist"},
Charger = {"fling"}
}
local tools = toolMapping[mutationName]
if tools then
for _, toolName in ipairs(tools) do
local tool = ServerStorage.ZombieTools:FindFirstChild(toolName)
if tool then
tool:Clone().Parent = player.Backpack
else
warn("Tool not found: " .. toolName)
end
end
end
chances:FireClient(player, mutationName, mutations)
end
local function handleTeamChange(player)
if player.Team == zombieteam then
if player.Character then
player.Character:Destroy()
end
spawnZombieWithAccessories(player)
else
if player.Character then
player.Character:Destroy()
end
player:LoadCharacter()
end
end
local function onPlayerAdded(player)
if player.Team == zombieteam then
spawnZombieWithAccessories(player)
end
player:GetPropertyChangedSignal("Team"):Connect(function()
handleTeamChange(player)
end)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Once(function()
task.wait(spawntime)
if not player.Character then
handleTeamChange(player)
end
end)
end)
end
players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(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)