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

HELP!? Issue with Chat Command - Adding tool to all players backpack

I'm trying to add a tool to all of the players backpack but it's not working? I've tried using WaitForChild on "Backpack", and more, but it doesn't seem to work or show an error when executed. Any idea what's wrong?

Code:

local ServerStorage = game:GetService("ServerStorage")
local TKSword = ServerStorage:WaitForChild("TKSword")
local prefix = "!"
Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Msg)
		if Msg:sub(1,5) == prefix..'set ' then
			if Msg:sub(6) == "nof" then
				Player.CharacterAdded:Connect(function()
				local backpack = Player:WaitForChild("Backpack")
				TKSword:Clone().Parent = backpack
				end)
			end
		end
	end)
end)```

1 answer

✓ Accepted answer

To start off, if you are posting code, please use the lua formatting. This makes it a lot easier to read and understand what's going on. You can add the lua formatting by pressing the lua symbol when editting.

The reason why your code does not work, is because you are only giving the tool when the player obtains a new character, meaning only when the player dies. You also only connected the function to the player who chatted, meaning that no other player would get TKSword.

You can fix this by looping over all players in game.Players, and only giving them the tool if they have a backpack. I've made the according adjustments in the code below.

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local TKSword = ServerStorage:WaitForChild("TKSword")
local prefix = "!"
local GiveTKSword = false

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Msg)
		if Msg:sub(1,5) == prefix..'set ' then
			if Msg:sub(6) == "nof" then
					GiveTKSword = true
				for _, Plr in pairs(game.Players:GetPlayers()) do
					if not Plr:FindFirstChild('Backpack') then continue end -- continue skips over all code in the for loop and continues on to the next player if it is executed, basically meaning that every player that does not have a backpack gets skipped over.
					TKSword:Clone().Parent = Plr.Backpack
				end
			end
		end
	end)

	Player.CharacterAdded:Connect(function()
		if GiveTKSword then
			if Player:FindFirstChild('Backpack') then
				TKSword:Clone().Parent = Player.Backpack
			end
		end
	end)
end)

2 comments

0

But won't they lose the tool when they reset? That's why I added CharacterAdded,

0

Using CharacterAdded when the command is executed it kinda weird, because that means that players that were not in the server upon execution would not receive the tool, and players that were in the server during 2 executions would receive 2 tools upon death. Instead, I'll update my posted code to provide a better solution.

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