Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my script misbehaving when it spawns players?

Asked by 4 years ago

I wrote a script that detects when a player spawns, and then (if they're a zombie in the context of my game) gives them clothes and puts them in the map. For some reason, players spawn with millions of shirts and pants and hats and that's not at all what I want. How can I make it so players spawn with only 1 outfit? Here's what the root looks like: https://imgur.com/a/vgrLv3o

local Players = game:GetService("Players")

local function onCharacterAdded(character)
    Player = game.Players:GetPlayerFromCharacter(character)
    wait(0.5)
    if Player.PlayerParameters.HumanOrZombie.Value == 0 then 
        -- no spawn required

    elseif Player.PlayerParameters.HumanOrZombie.Value == 1 then
        -- human!
        wait(0.5)
        survivorspawns = workspace:WaitForChild("SurvivorSpawns"):GetChildren()
        randomspawn = survivorspawns[math.random(1, #survivorspawns)]
        character:MoveTo(randomspawn.Position)

    elseif Player.PlayerParameters.HumanOrZombie.Value == 2 then
        -- zombie!
        for i,v in pairs(character:GetChildren()) do
            if v:IsA("Accessory") then
                v:Destroy()
            elseif v:IsA("BasePart") then
                v.BrickColor = BrickColor.new("Dark green")
            elseif v:IsA("Shirt") then
                v:Destroy()
            elseif v:IsA("Pants") then
                v:Destroy()
            elseif v:IsA("ShirtGraphic") then
                v:Destroy()
            end
            if game.ReplicatedStorage.MapName.Value == "Grassy Plains" then
                randomclothes = game.ServerStorage.ZombieClothes.GrassyPlains:GetChildren()
                pickedclothes = randomclothes[math.random(1, #randomclothes)]
                for i,x in pairs(pickedclothes:GetChildren()) do
                    x:Clone().Parent = character
                end
            end
        end
        wait(0.5)
        zombiespawns = workspace:WaitForChild("ZombieSpawns"):GetChildren()
        randomspawnn = zombiespawns[math.random(1, #zombiespawns)]
        character:MoveTo(randomspawnn.Position)
    end
    if game.ReplicatedStorage.RoundBeginning.Value == true then
        character.Humanoid.WalkSpeed = 0
    end
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

1 answer

Log in to vote
0
Answered by 4 years ago

Basically, you are getting clothing for the zombie every time it loops through a piece of the character because you have it in the for loop.

Removing it from the for loop should make it only clone one pair of clothing.

local Players = game:GetService("Players")

local function onCharacterAdded(character)
    Player = game.Players:GetPlayerFromCharacter(character)
    wait(0.5)
    if Player.PlayerParameters.HumanOrZombie.Value == 0 then 
        -- no spawn required

    elseif Player.PlayerParameters.HumanOrZombie.Value == 1 then
        -- human!
        wait(0.5)
        survivorspawns = workspace:WaitForChild("SurvivorSpawns"):GetChildren()
        randomspawn = survivorspawns[math.random(1, #survivorspawns)]
        character:MoveTo(randomspawn.Position)

    elseif Player.PlayerParameters.HumanOrZombie.Value == 2 then
        -- zombie!
        for i,v in pairs(character:GetChildren()) do
            if v:IsA("Accessory") then
                v:Destroy()
            elseif v:IsA("BasePart") then
                v.BrickColor = BrickColor.new("Dark green")
            elseif v:IsA("Shirt") then
                v:Destroy()
            elseif v:IsA("Pants") then
                v:Destroy()
            elseif v:IsA("ShirtGraphic") then
                v:Destroy()
            end
        end

    if game.ReplicatedStorage.MapName.Value == "Grassy Plains" then --//I just moved this if statement out of the loop
             randomclothes = game.ServerStorage.ZombieClothes.GrassyPlains:GetChildren()
             pickedclothes = randomclothes[math.random(1, #randomclothes)]
             for i,x in pairs(pickedclothes:GetChildren()) do
                 x:Clone().Parent = character
             end
         end

        wait(0.5)
        zombiespawns = workspace:WaitForChild("ZombieSpawns"):GetChildren()
        randomspawnn = zombiespawns[math.random(1, #zombiespawns)]
        character:MoveTo(randomspawnn.Position)
    end
    if game.ReplicatedStorage.RoundBeginning.Value == true then
        character.Humanoid.WalkSpeed = 0
    end
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)
0
Thank you! I didn't really know that it was a loop, I thought it was just some function Casual_Keldeo 4 — 4y
0
No problem, glad I could help! CeramicTile 847 — 4y
Ad

Answer this question