Detecting Tool, Deletion and tool value Problem (Tool2 doesn't get deleted or get the right value)

ive been working on a delivery system and the script works only for “Donut” not for “chocolateDonut” tought…

local Dummy1 = workspace.Dummy1
local npc = script.Parent  -- Assumes the script is inside the NPC

local toolNames = {"Donut", "ChocolateDonut"}  -- Add the names of your tools

-- Function to handle proximity prompt activation
local function onPromptActivated(player)
	local backpack = player.Backpack
	if backpack then
		local cashStat = player:FindFirstChild("leaderstats")

		if not cashStat then
			warn("Leaderstats not found for player " .. player.Name)
			return
		end

		for _, toolName in pairs(toolNames) do
			local tools = backpack:GetChildren()

			for _, tool in pairs(tools) do
				if tool:IsA("Tool") and tool.Name == toolName then
					local valueObject = tool:FindFirstChild("Value")

					if valueObject and valueObject:IsA("NumberValue") then
						local cashAmount = valueObject.Value

						local cashValue = cashStat:FindFirstChild("Cash")
						if not cashValue then
							warn("Cash value not found in leaderstats for player " .. player.Name)
							return
						end

						-- Add cash directly to the player's stat (modify this according to your game)
						cashValue.Value = cashValue.Value + cashAmount

						-- Display an overhead GUI with the cash amount over the NPC's head
						local gui = Instance.new("BillboardGui")
						gui.Size = UDim2.new(0, 200, 0, 50)
						gui.Adornee = Dummy1  -- Change 'npc' to the reference of your NPC
						gui.StudsOffset = Vector3.new(0, 2, 0)

						local text = Instance.new("TextLabel")
						text.Text = "Received $" .. cashAmount
						text.Size = UDim2.new(1, 0, 1, 0)
						text.TextScaled = true  -- Adjusted to improve text clarity
						text.TextStrokeTransparency = 0.5
						text.Parent = gui

						gui.Parent = game.Workspace
						wait(3)  -- Display the GUI for 3 seconds (adjust as needed)
						gui:Destroy()

						tool:Destroy()
					end
				end
			end
		end
	end
end

-- Create the proximity prompt
local npc = script.Parent  -- Assumes the script is inside the NPC
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Deliver"
prompt.Parent = npc
prompt.Triggered:Connect(onPromptActivated)
1 Like

Ive been having this problem for a while the script its also inside an npc.

Hey, It Is a bit hard to understand the text what Is wrong with the script? Does It give you any errors?

This Is the slightly modified version of your script with additional debugging lines to help identify the issue:

local npc = script.Parent  --the script is inside the NPC
local toolNames = {"Donut", "ChocolateDonut"}

local function onPromptActivated(player)
	local backpack = player:FindFirstChild("Backpack")
	if not backpack then
		warn("Backpack not found for player " .. player.Name)
		return
	end

	local cashStat = player:FindFirstChild("leaderstats")
	if not cashStat then
		warn("Leaderstats not found for player " .. player.Name)
		return
	end

	for _, toolName in pairs(toolNames) do
		for _, tool in pairs(backpack:GetChildren()) do
			if tool:IsA("Tool") and tool.Name == toolName then
				local valueObject = tool:FindFirstChild("Value")
				if not valueObject or not valueObject:IsA("NumberValue") then
					warn(toolName .. " does not contain a valid 'Value' NumberValue")
					return
				end

				local cashAmount = valueObject.Value
				local cashValue = cashStat:FindFirstChild("Cash")
				if not cashValue then
					warn("Cash value not found in leaderstats for player " .. player.Name)
					return
				end

				cashValue.Value = cashValue.Value + cashAmount

				local gui = Instance.new("BillboardGui", workspace)
				gui.Size = UDim2.new(0, 200, 0, 50)
				gui.Adornee = npc
				gui.StudsOffset = Vector3.new(0, 2, 0)

				local text = Instance.new("TextLabel", gui)
				text.Text = "Received $" .. cashAmount
				text.Size = UDim2.new(1, 0, 1, 0)
				text.TextScaled = true
				text.TextStrokeTransparency = 0.5

				wait(3)
				gui:Destroy()

				tool:Destroy()
				print(toolName .. " processed and destroyed for " .. player.Name)
			else
				print("Tool not found or mismatch: " .. toolName)
			end
		end
	end
end

local prompt = Instance.new("ProximityPrompt", npc)
prompt.ActionText = "Deliver"
prompt.Triggered:Connect(onPromptActivated)

1 Like