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

[SOLVED]Clones more than 1 wep when a person has the same value?

Asked by 9 years ago
for i, plr in ipairs(game.Players:GetChildren()) do
        if plr.leaderstats.value.Value == 1 then
            for i, player in pairs(game.Players:GetChildren()) do
                if player:FindFirstChild("Backpack")
                    then
                SS.SWeps.Sword:clone().Parent = player:FindFirstChild("Backpack")
            end
        end
    end
end

When 2 or more people have the same leaderboard value, it clones 2 or more swords depending on how many people have the same value.

EX: When 3 people have the same leaderboard value it clones 3 swords into the peoples backpack instead of 1.

2 answers

Log in to vote
3
Answered by
DeadToMe 105
9 years ago

You don't need the second for loop; that is what is getting you the multiple swords. The second if statement also may slow it down a little bit, and is unneeded.

for i, plr in ipairs(game.Players:GetPlayers()) do
    if plr.leaderstats.value.Value == 1 then
        SS.SWeps.Sword:clone().Parent = plr:WaitForChild("Backpack")
    end
end
0
Thanks! rabidhalofan12345 55 — 9y
0
No problem. DeadToMe 105 — 9y
Ad
Log in to vote
3
Answered by
Muoshuu 580 Moderation Voter
9 years ago

Try this code:

for i,Player in pairs(game.Players:GetChildren()) do
    if Player.leaderstats.value.Value==1 then
        if Player:FindFirstChild("Backpack") then
            SS.SWeps.Sword:Clone().Parent=Player:FindFirstChild("Backpack")
        end
    end
end

Explanation: You were using two 'for' loops, meaning for every player, you'd loop through each player and add a tool to their backpack

I removed the second 'for' loop so that it only gives the player a sword if their Value is equal to 1

0
I appreciate this. Thanks! rabidhalofan12345 55 — 9y

Answer this question