Box Delivery System

I’m trying to make a box delivery system, but it’s not working :frowning: Could someone help me? I made a tool called “Green Box”, the right thing would be, whenever I spend it with the part, I would earn 10 Money and it would disappear from my inventory, but when I pass it through the part I do not earn 10 money .

local Players = game:GetService("Players")
local dinheiroChunk = script.Parent

local function onPartTouch(hum)
	local partParent = hum.Parent
	local h = partParent:FindFirstChild("Humanoid")			
	local backpack = Players.LocalPlayer.BackPack
	local box = backpack:FindFirstChild("Green Box")
	
	if box then
		local player = Players:GetPlayerFromCharacter(partParent)
		local leaderstats = player.leaderstats
		local dinheiroStats = leaderstats:FindFirstChild("Dinheiro")
		if dinheiroStats then
			dinheiroStats.Value = dinheiroStats.Value + 10 	
		end
	end    
end

dinheiroChunk.Touched:Connect(onPartTouch)

image

You should make appropriate checks to see if what the part is touching is the player.

local Players = game:GetService("Players")
local dinheiroChunk = script.Parent

local function onPartTouch(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
    if player == nil then return end
	local h = hit.Parent:FindFirstChild("Humanoid")			
	local backpack = player.Backpack
	local box = backpack:FindFirstChild("Green Box") or hit.Parent:FindFirstChild("Green Box")
	
	if box then
		local leaderstats = player.leaderstats
		local dinheiroStats = leaderstats:FindFirstChild("Dinheiro")
		if dinheiroStats then
			dinheiroStats.Value = dinheiroStats.Value + 10 	
		end
	end    
end

dinheiroChunk.Touched:Connect(onPartTouch)
1 Like