Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

1 comment

0

Both tools have already been placed in ServerStorage.

1 answer

✓ Accepted answer

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!

3 comments

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?

0

Nevermind, I figured it out. Thanks for the help!

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.