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

Why does the tool not get placed inside of a player's backpack?

Asked by 3 years ago

I need two different swords to be put into the player's backpacks, based on what team they are on. There is no error or warning, the script just doesn't do anything.

    local RedCount = game.Teams.Red:GetPlayers()
    local BlueCount = game.Teams.Blue:GetPlayers()

    local RedSword = game.ServerStorage.RedSword
    local BlueSword = game.ServerStorage.BlueSword

    for i,v in pairs(RedCount) do
        local ClonedSword = RedSword:Clone()
        ClonedSword.Parent = game.Players[v].Backpack
    end

    for i,v in pairs(BlueCount) do
        local ClonedSword = BlueSword:Clone()
        ClonedSword.Parent = game.Players[v].Backpack
    end

Please reply with a response or a question.

Thanks!

0
Both tools have already been placed in ServerStorage. ChickwensRule 2 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hello.

The reason as to why your swords aren't cloning to the player's backpack is because you aren't defining the player properly. Using a in pairs loop will not define the player within the team, so we have to find a way around that. You can easily do this by calling the Player with the PlayerAdded function. After that, we can check what team the player is in, in our case it's either Red or Blue. From that we can determine what swords should be cloned to their backpack depending on their teams.

Here is how you would do it with this method:

local Teams = game:GetService("Teams")
local ServerStorage = game:GetService("ServerStorage")

local RedTeam = Teams.Red
local BlueTeam = Teams.Blue

local RedSword = ServerStorage.RedSword
local BlueSword = ServerStorage.BlueSword

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character) -- This has no use in the current script, however it makes sure that the Character is loaded in to prevent failure to clone the items.
        if Player.Team == RedTeam then
            local ClonedSword = RedSword:Clone()
            ClonedSword.Parent = Player.Backpack
        end
        if Player.Team == BlueTeam then
            local ClonedSword = BlueSword:Clone()
            ClonedSword.Parent = Player.Backpack
        end
    end)
end)

Hope this helps!

0
The script still doesn't work, even after I defined the player properly. This code is running inside of another function, which is being run inside of main loop. Is there a way so that I can still define the player correctly while still having this run properly? ChickwensRule 2 — 3y
0
Nevermind, I figured it out. Thanks for the help! ChickwensRule 2 — 3y
0
No problem! RazzyPlayz 497 — 3y
Ad

Answer this question