Tools will not go to player backpack after choosing a team

I’m developing a shooter game and I want to have team specific weapons and flags and whatever. Problem is, when I try to put a tool into a player backpack, it doesn’t work.
Scripts:

local plr=script.Parent.Parent.Parent.Parent.Parent.Name

local Defend = game.ReplicatedStorage.DefendEvent

script.Parent.MouseButton1Click:connect(function()

game.Players[plr].TeamColor=BrickColor.new("Really blue")

game.Workspace[plr].Humanoid.Health="0"

game.StarterGui.TeamSelectGUI.Frame.Visible = false

Defend:FireClient()

end)
local Flag = game.ServerStorage.LMWeapons.LMFlags.LMFlag

local Defend = game.ReplicatedStorage.DefendEvent


game.Players.PlayerAdded:Connect(function(player)
	Defend.FireClient:Connect(function()
		Flag:Clone()
		Flag.Parent = player.Backpack
	end)
end)

maybe if you remove the player added event and move the player argument to the remote fired event it will work.

1 Like

I think you may have to wait for the character to load. Backpack resets for every time player resets its character. You should also place the cloned object into a variable first, this may cause a bug.

1 Like

The two scripts have a lot of flaws, the tool not cloning wasn’t probably the only issue. I tried fixing all the bugs I was able to find:

--Server script(script 2)
local Flag = game.ServerStorage.LMWeapons.LMFlags.LMFlag

local Defend = game.ReplicatedStorage.DefendEvent

--The event should not be inside PlayerAdded(it will cause it to misbehave)
--when listening on the server you must use OnServerEvent(also FireClient is a function not an event) 
Defend.OnServerEvent:Connect(function(player)
	--the team must be changed on the server to replicate
	player.TeamColor = BrickColor.new("Really blue")
	--Health accepts numbers not strings
	player.Humanoid.Health = 0 
	--you must parent the clone not the original
	local clone = Flag:Clone()
	clone.Parent = player.Backpack 
end)
--LocalScript(script 1)
local Defend = game.ReplicatedStorage.DefendEvent

script.Parent.MouseButton1Click:Connect(function()
	--I assume the UI resets on spawn? so I leave that as it is
	game.StarterGui.TeamSelectGUI.Frame.Visible = false
	--:FireServer() must be used to communicate with a server script(not FireClient)
	Defend:FireServer()
end)
1 Like