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

How to remove specific item from inventory when player touches an part?

this is what i have so far

script.Parent.Touched:connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid then local player = game.Players:FindFirstChild(hit.Parent.Name)

for i=1, 1 do

for i,v in pairs(game.Players:GetPlayers()) do 
for a,b in pairs(v.Character:GetChildren()) do 
		if b:IsA("Tool") or b:IsA("HopperBin") then 
        b:Destroy()

end end end end v.Backpack:ClearAllChildren() end end)

in this script every item gets destroyed so far i knowe. I'm a big noob on coding so I hope someone can help me.

Item that i want to remove from players inventory is called "Blue Master Key"

3 comments

0

this is for roblox game

0

And why destroy all the tools after removing a specific one?

3 answers

✓ Accepted answer
function onClicked(Plr)
	local Char = Plr.Character
	if Char:FindFirstChild("Blue Master Key", true) then
		Char:FindFirstChild("Blue Master Key", true):Destroy() -- Removes tool from players hands
	end
	if Plr.Backpack:FindFirstChild("Blue Master Key", true) then 
		Plr.Backpack:FindFirstChild("Blue Master Key", true):Destroy() -- Removes tool from players backback
	end
end

2 comments

0

Character is not a valid member of Script & Stack Begin - Script 'Workspace.Model.Diamond.Part.Teleport', Line 4 - Stack End - Request was throttled. Try sending fewer requests.

0

i added "script.Parent.ClickDetector.MouseClick:connect(onClicked)" afer last end and it works perfect! Thanx ! Realy aprishiate your help!

Hello, kexiGamer!

This script completly removes the key from the player! Removes from player hand and player inventory =D

Hope you like it!

script.Parent.Touched:connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		for i,item in pairs(hit.Parent:GetChildren()) do
			if item.Name == "Blue Master Key" and item:IsA("Tool") then
				item:Destroy()
			end
		end
		for i,item in pairs(game.Players[hit.Parent.Name].Backpack:GetChildren()) do
			if item.Name == "Blue Master Key" and item:IsA("Tool") then
				item:Destroy()
			end
		end
	end
end)

Good luck with your games!

Hi there and thanx for your answer I get: "Request was throttled. Try sending fewer requests. Key = 551800471 (x4) " and (x4) is just growing and growing. No item dissapears from inventory.

Here is my the script that is working but it is not removing only "BlueMasterKey" tool. It is clearing whole inventory.

function onClicked() 
for i=1, 1 do-----while wait(5) can allso be used and it is working (all other script that i tryed here seems not to work)
for i,v in pairs(game.Players:GetPlayers()) do 
    for a,b in pairs(v.Character:GetChildren()) do 
        if b:IsA("Tool") or b:IsA("HopperBin") then 
            b:Destroy()
        end
    end
    v.Backpack:ClearAllChildren()
end
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Is it possible to start script like this and continue writing so only the "Blue Master Key" gets removed from inventory?

function onClicked()
for i=1, 1 do
-------ADD CODE TO ONLY REMOVE "Blue Master Key" ---->

end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

I realy aprishiate your help and time!

1 comment

0

ClearAllChildren removes all children from the item. Just simply remove it to prevent that.

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