Tool isn't going to the player

Hello Developers.

For the past 20 minutes I been thinking on cool ways to give players tools without them using admin commands. I thought about making it so if they message something it would give them a tool but its quiet not working how I wanted it to. there’s no errors in the output its not giving the player the tool.

The Code:

local RepStorage = game.ReplicatedStorage
local HasFootball = game.StarterPlayer.StarterCharacterScripts.Scripts.Values:WaitForChild("HasFootball")
local Football = RepStorage.Objects:WaitForChild("Football")

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Text)
		
		if Text:sub(1, 4) == ":ball" then
			local Tool
			for i,v in pairs(RepStorage.Objects:GetChildren()) do
				local ToolName = string.lower(v.Name)

				if string.find(Text, ToolName) then
					Tool = v.Name
				end
			end
			local Giver
			for i,Player in pairs(game.Players:GetPlayers()) do
				local PlayerName = string.lower(Player.Name)

				if string.find(Text, PlayerName) then
					Giver = Player
				end
			end
			if Tool and Giver then
				local BackPack = Giver.BackPack
				local AddedTool = RepStorage.Objects:WaitForChild("Football"):Clone()
				AddedTool.Parent = Player.BackPack
				end
			end
		end)
end)

Likely because here you’re getting the first 4 characters and comparing it to a 5 character word, maybe change the Text:sub(1,4) to Text:sub(1,5). You may also want to lowercase in the event you add some capitalization and prevents it from working

Thanks for the suggestion but it still doesn’t give me the tool

I think it may be something to do with how you’re setting it up. Maybe you could split the string up using string.split?

But firstly, try adding print statements inside the if statements to see where it’s not detecting what you wrote, likely something could be wrong with wriitng the command itself?

Also another thing

Did you mean to do

local AddedTool = RepStore.Objects[Tool]:Clone()

Because you’re allowing the player to give a tool name but only expecting a football, did you mean to not include the tool name?

Still didn’t work. Thanks for the suggestion though

Okay perhaps try this?

local RepStorage = game.ReplicatedStorage
local HasFootball = game.StarterPlayer.StarterCharacterScripts.Scripts.Values:WaitForChild("HasFootball")
local Football = RepStorage.Objects:WaitForChild("Football")

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Text)
		local splits = Text:lower():split(" ")
		
		if splits[1] == ":ball" then
			print("Detected command")
			local Tool
			for i,v in pairs(RepStorage.Objects:GetChildren()) do
				local ToolName = string.lower(v.Name)

				if splits[2] == ToolName then
					Tool = v.Name
					break
				end
			end
			print(Tool)
			local Giver
			for i,Player in pairs(game.Players:GetPlayers()) do
				local PlayerName = string.lower(Player.Name)

				if splits[3] == PlayerName then
					Giver = Player
					break
				end
			end
			print(Giver)
			if Tool and Giver then
				local AddedTool = RepStorage.Objects[Tool]:Clone()
				AddedTool.Parent = Giver.BackPack
			end
		end
	end)
end)

Something is defintely up with how the code is set up. This will detect the first word as the comman,d the 2nd as the name of the tool and the 3rd as the player to give the ball to.

But I think you just want to give the ball to yourself, judging by how even though you give ita player and a tool, it’s still going to give a football to you only, so can’t you do this?

local RepStorage = game.ReplicatedStorage
local HasFootball = game.StarterPlayer.StarterCharacterScripts.Scripts.Values:WaitForChild("HasFootball")
local Football = RepStorage.Objects:WaitForChild("Football")

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Text)		
		if Text:lower() == ":ball" then
			print("Detected command")
			Football:Clone().Parent = Player.BackPack
		end
	end)
end)

Also I don’t think your HasFootball variable is going to do anything, you’d have to get the HasFootball value manually by going to the character via Player.Character instead of going to StarterCharacterScripts

I wanted to so something like. If you have a football in your backpack the value would check. and make it so you can’t give your self more then 1 football

So it detected the command. But its not giving the player anything

I think it may because of my typo, Player.BackPack should be Player.Backpack

The typo didn’t do anything to the script

Okay it’s probably because you’re using the other version that doesn’t sutio your needs then, use this

local RepStorage = game.ReplicatedStorage
local HasFootball = game.StarterPlayer.StarterCharacterScripts.Scripts.Values:WaitForChild("HasFootball")
local Football = RepStorage.Objects:WaitForChild("Football")

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Text)		
		if Text:lower() == ":ball" then
			print("Detected command")
			Football:Clone().Parent = Player.Backpack
		end
	end)
end)

For detecting if the character already has one, check their character using FindFirstChildOfClass("Tool") and check if the tool’s name is Football. And also check their Backpack if they alreayd have a tool called Football

What line do you want me to to replace with this I’m confused

You can delete the entire code and just paste the code I sent and that should be all you need to do really