Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help with a touched event to destroy a player's tool?

So I was trying to make something when if you touch something with a tool it will get taken out of your inventory and placed into the shrine. However, it will not work



local event = game.ReplicatedStorage.event
local function touched()
	script.Parent.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Beans") then
			script.Parent.Parent.BeanPosition.Transparency = 0
			print("Player has beans!")
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			print(player.Name.." needs their beans removed.")
			local light = script.Parent.Parent.Carpet.SurfaceLight
			light.Enabled = true
			player.Backpack.Beans:Destroy()
			script.Parent.Parent.BeanPosition.Script.Disabled = true
		end
	end)
end

while wait(5) do
	spawn(touched)
end



4 comments

0

:(

0

what?

0

Have you checked the output?

0

yes, it says "Beans is not a valid member of Backpack" when it's in the player's inventory.

1 answer

✓ Accepted answer

Hello, If the player has the item equipped, then the tool is in their character.. please try the script below.

local event = game.ReplicatedStorage.event
local function touched()
    script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Beans") then
            script.Parent.Parent.BeanPosition.Transparency = 0
            print("Player has beans!")
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            print(player.Name.." needs their beans removed.")
            local light = script.Parent.Parent.Carpet.SurfaceLight
            light.Enabled = true
            hit.Parent.Beans:Destroy()
            script.Parent.Parent.BeanPosition.Script.Disabled = true
        end
    end)
end

while wait(5) do
    spawn(touched)
end

Please upvote if this works, Thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.