Hi guys. Would greatly appreciate some help with the following issue:
-
My code is supposed to detect a tool in the player’s inventory, delete that tool then give the player another tool
-
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.
-
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”.
-
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)