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!
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!