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

How do I prevent this code from running 6 times?

Asked by 5 years ago
Edited 5 years ago

I used debounce but I need this code to be able to only run once yet it runs 6 times?

Is there any solution to this? Thanks.

Image

This script clones instances from replicated storage into the player, it does it 6 times and not once, any fix to only do it once?

local debounce = true

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
    if debounce then
    debounce = false
    local particle1 = game.ReplicatedStorage.ParticleEmitter
    local particle2 = game.ReplicatedStorage.ParticleEmitter2
    local Sound = game.ReplicatedStorage.Sound
    local Velocity = game.ReplicatedStorage.BodyVelocity
    local death = game.ReplicatedStorage.death
    local theCharacter = workspace:WaitForChild(player.Name)
    local ragdoll = game.ReplicatedStorage.ragdoll
        for i,v in pairs(character:GetChildren()) do
            if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then
                if theCharacter.Humanoid ~= nil then
                particle1:Clone().Parent = theCharacter.Torso
                particle1:Clone().Parent = theCharacter.Head
                particle1:Clone().Parent = theCharacter["Left Arm"]
                particle1:Clone().Parent = theCharacter["Right Arm"]
                particle1:Clone().Parent = theCharacter["Left Leg"]
                particle1:Clone().Parent = theCharacter["Right Leg"]
                particle2:Clone().Parent = theCharacter.Torso
                particle2:Clone().Parent = theCharacter.Head
                particle2:Clone().Parent = theCharacter["Left Arm"]
                particle2:Clone().Parent = theCharacter["Right Arm"]
                particle2:Clone().Parent = theCharacter["Left Leg"]
                particle2:Clone().Parent = theCharacter["Right Leg"]
                death:Clone().Parent = theCharacter
                Velocity:Clone().Parent = theCharacter.Torso
                Velocity:Clone().Parent = theCharacter.Head
                Velocity:Clone().Parent = theCharacter["Left Arm"]
                Velocity:Clone().Parent = theCharacter["Right Arm"]
                Velocity:Clone().Parent = theCharacter["Left Leg"]
                Velocity:Clone().Parent = theCharacter["Right Leg"]
                ragdoll:Clone().Parent = theCharacter
                Sound:Clone().Parent = theCharacter.Torso
                wait(4)
                debounce = true
                    end
                end
            end
        end
    end)
end)
0
This code needs a lot of work. Wow. User#21908 42 — 5y
0
Well I wasn't the one who fixed it (some other user helped fix it) LoganboyInCO 150 — 5y

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

The reason why this cloning multiple instances into the limbs is because you have over 15 lines of Cloning the particle/velocity instances and parenting them.

I can start by explaining (in comments) how your for-loop is working:

--So we get the children of our character.
--This includes all scripts, humanoid and limbs.
for i,v in pairs(character:GetChildren()) do

    --Now we check if 'v' is a basepart and not a HRP. 
    --This should be 6 parts for a R6 character or 15 for R15.
    if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then

        --You also had a redundant check for the humanoid.
        --The last if statement should of handled that.
    end
end

So this for loop should loop either 6 or 15 times if the character is either an R6 or R15 character. If you don't get that then you need to test how GetChildren() and for-loops work some more.

Now returning to the issue: You have over 15+ lines of cloning and parenting instances into each of these limb parts (End result is your image example)! You should instead remove those lines and simplify this down into two lines.

Since v should be a BasePart then this should look like:

for i,v in pairs(character:GetChildren()) do
    if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then
        local newParticle1 = particle1:Clone()
        newParticle1.Parent = v --This clones and parents to the basepart

        local newParticle2 = particle2:Clone()
        newParticle2.Parent = v --same again

        local newDeath = death:Clone()
        newDeath.Parent = v --same

        local newVel = velocity:Clone()
        newVel.Parent = v --same


    end
end

You see how it's more readable?

Now we can clean up your full script by making some of your variables into global vars and defining services into variables.

Final Script:


--Define services. Makes it easier to read variables local RS = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") --That debounce wasn't needed either --You could use WaitForChild here. --Server side so really doesn't matter to me. local particle1 = RS.ParticleEmitter local particle2 = RS.ParticleEmitter2 local sound = RS.Sound local velocity = RS.BodyVelocity local death = RS.death local ragdoll = RS.ragdoll Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) --CharacterAdded provides you with the character --You don't need to set a variable for it --You could wait until it's loaded though! for i,v in pairs(character:GetChildren()) do if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then local newParticle1 = particle1:Clone() newParticle1.Parent = v --This clones and parents to the basepart local newParticle2 = particle2:Clone() newParticle2.Parent = v --same again end end local newDeath = death:Clone() newDeath.Parent = character local newRagDoll = ragdoll:Clone() newRagDoll.Parent = character local newVel = velocity:Clone() newVel.Parent = character local newSound = sound:Clone() sound.Parent = character.HumanoidRootPart --test this end) end)

Edit: formatting. Fixed variable name inside of for loop in final script.

0
It works well but the thing is death script, sounds, and ragdoll script should not be inserted in the players limbs, only in the players model. LoganboyInCO 150 — 5y
0
Then you would put those outside of the for loop since you want then cloned once. Final Script was edited. xPolarium 1388 — 5y
0
I see. Makes sense now. Thank you. LoganboyInCO 150 — 5y
Ad

Answer this question