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

I have a small problem with this simple script?

I have a script that I want that when the player enters a Team I gave him a Weapon but if the player changes his team I want him to only have the weapon that will be granted when he enters the Team .... What I did in the script is that when the player enters the Team I gave him a Weapon but at the same time I eliminated the player's Backpack, okay I try the script and everything is fine but my problem with that script is that when the player's Backpack is removed the weapon appears that is granted when entering the Team but as Arma [2]. So I went to a Team and they gave me the weapon, then I changed Team and they gave me the other Team's weapon and they eliminated the Weapon I had, okay then it is supposed that the first weapon entered into my 'Backpack' is the one that gives me the first Team I joined; When I enter the other Team, they give me the weapon they give me as a secondary. I hope you understand me Thanks for reading!

It is a Normal Script and it is located in the Workspace .

print("hola")
local teams = game:GetService("Teams")
local Team1 = game.Teams.Police 
local Team2 = game.Teams.Criminal
local Tools1 = game.ReplicatedStorage.PoliceTools
local Tools2 = game.ReplicatedStorage.CriminalTools

function Tools()
       for _,v in pairs(Tools1:GetChildren())do
          v:Clone().Parent = Team1
end
       for _,v in pairs(Tools2:GetChildren())do
          v:Clone().Parent = Team2
   end
end Tools()
local function GetTools1(plr)
		for _,v in pairs(Team1:GetChildren())do
		local Tools = v:Clone()
		Tools.Parent = plr:WaitForChild("Backpack")
		for i ,v in pairs(plr:WaitForChild("Backpack"):GetChildren())do
		  if v ~= Tools then
			v:Remove()
		end 
   end
		end
		end

	local function GetTools2(plr)
		for _,v in pairs(Team2:GetChildren())do
		local Tools = v:Clone()
		Tools.Parent = plr:WaitForChild("Backpack")
		for i ,v in pairs(plr:WaitForChild("Backpack"):GetChildren())do
		  if v ~= Tools then
			v:Remove()
		end 
   end
		end
		end

local function JoinedTeam1(player)
	print(player.Name, "joined the team 1")

	GetTools1(game.Players.LocalPlayer)
end
local function JoinedTeam2(player)
	print(player.Name, "joined the team 2")

	GetTools2(game.Players.LocalPlayer)
end
 
Team2.PlayerAdded:Connect(JoinedTeam2)
Team1.PlayerAdded:Connect(JoinedTeam1)

1 comment

0

that was a very confusing paragraph.

2 answers

✓ Accepted answer

You are on the right track, however the reason your code may not work as expected, is because you are removing the tools at the same time as adding new ones.

To fix this, before adding tools use player.Backpack:ClearAllChildren() in order to get rid of any existing tools.

Also, in order to clean your code a bit, you can create a single function which handles iterating through a given container and copying the children to the player's backpack. In my snippet below, that is AddTools.

After clearing the children, check which team the player has been added to, and call AddTools to give them the appropriate gear.

local teams = game:GetService("Teams")
local Team1 = game.Teams.Police 
local Team2 = game.Teams.Criminal
local Tools1 = game.ReplicatedStorage.PoliceTools
local Tools2 = game.ReplicatedStorage.CriminalTools

local function AddTools(backpack, tools)
	for _, tool in pairs(tools) do
		tool:Clone().Parent = backpack
	end
end

local function ChangeTeam(player)
	local backpack = player.Backpack
	backpack:ClearAllChildren()
	if player.Team == Team1 then
		AddTools(backpack, Tools1)
	elseif player.Team == Team2 then
		AddTools(backpack, Tools2)
	end
end
	
Team1.PlayerAdded:Connect(ChangeTeam)
Team2.PlayerAdded:Connect(ChangeTeam)

Let me know if you have any questions or if you try this and it doesn't work as expected.

2 comments

0

You need to explain what you fixed, how you fixed it and how the code works. Just providing the correct answer doesn't help them since it will only confuse them more.

0

Thank you, I have edited my answer to include an explanation.

In line 43, you’re using LocalPlayer which is nil on the server.

And on lines 23 and 34, it should be :Destroy(), not :Remove().

1 comment

0

It seems that you have misunderstood friend, the script works well, the thing is that the tool is put in tool [2]

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