Forward Slash String Bug

Hi guys. Would greatly appreciate some help with the following issue:

  1. My code is supposed to detect a tool in the player’s inventory, delete that tool then give the player another tool

  2. My code will detect “HamSandwich”, “ChickenSandwich” and “Turkeysandwich” without issue and will delete and replace them with the correct tools. The script functions as intended to this point.

  3. My code refuses to detect any other item (all other items have a / in the name i.e. “HamSandwich/LOT”, and keeps spitting out an error that “HamSandwich is not a valid member of workspace.nd89”.

  4. Any help would be fantastic! See code snippet below-

-- declare services --
local serverstorage = game:GetService("ServerStorage")

-- declare variables --
local proxprompt = script.Parent
local haml = serverstorage:WaitForChild("HamSandwich/T", 20)
local hamlo = serverstorage:WaitForChild("HamSandwich/OT", 20)
local hamlt = serverstorage:WaitForChild("HamSandwich/LT", 20)

local function checkfortool(player, toolname)
	
	local character = player.Character

	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Tool") then
			print(v.Name)
			if string.match(v.Name, toolname) then
				return true;
			end
		end
	end
	return false;
end

proxprompt.Triggered:Connect(function(player)
	
	local character = player.Character

	if checkfortool(player, "HamSandwich") == true then
		print("give food")
		character["HamSandwich"]:Destroy()
		local coffeeclone = haml:Clone()
		coffeeclone.Parent = character
		
	elseif checkfortool(player, "HamSandwich/O") == true then
		print("give food")
		character["HamSandwich/O"]:Destroy()
		local coffeeclone = hamlo:Clone()
		coffeeclone.Parent = character
		
	elseif checkfortool(player, "HamSandwich/L") == true then
		print("give food")
		character["HamSandwich/L"]:Destroy()
		local coffeeclone = hamlt:Clone()
		coffeeclone.Parent = character

else
		print("no can do")
	end
end)



Is it supposed to check only if the tool is equipped or is it in the inventory?

Try this code and see if it works.

-- declare services --
local serverstorage = game:GetService("ServerStorage")

-- declare variables --
local proxprompt = script.Parent
local haml = serverstorage:WaitForChild("HamSandwich/T", 20)
local hamlo = serverstorage:WaitForChild("HamSandwich/OT", 20)
local hamlt = serverstorage:WaitForChild("HamSandwich/LT", 20)

local function checkfortool(player, toolname)
	
	local character = player.Character

	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Tool") then
			print(v.Name)
			if v.Name == toolname then
				return true;
			end
		end
	end
	return false;
end

proxprompt.Triggered:Connect(function(player)
	
	local character = player.Character
    -- Removed [ == true] for every if statement.
	if checkfortool(player, "HamSandwich") then
		print("give food")
		character["HamSandwich"]:Destroy()
		local coffeeclone = haml:Clone()
		coffeeclone.Parent = character
		
	elseif checkfortool(player, "HamSandwich/O") then
		print("give food")
		character["HamSandwich/O"]:Destroy()
		local coffeeclone = hamlo:Clone()
		coffeeclone.Parent = character
		
	elseif checkfortool(player, "HamSandwich/L") then
		print("give food")
		character["HamSandwich/L"]:Destroy()
		local coffeeclone = hamlt:Clone()
		coffeeclone.Parent = character

else
		print("no can do")
	end
end)

1 Like

This solved the issue.

You’re a legend mate, cheers.

1 Like