✓ 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
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
But won't they lose the tool when they reset? That's why I added CharacterAdded,
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.