Script letting you pick up the same tool more than once in your inventory

Hello,

So I have a script that’s for picking up tools with a proximity prompt. You’re supposed to only carry 1 at the same time but it’s a bit buggy right now. Here’s a clip of what I mean:

As you can see, it’s letting me pick up more than 1 of the same tool at once in the inventory. Doesn’t do this when the tool is equipped though but as soon as you do and try again, it duplicates?

The set up:

Dummy key/mesh in a Workspace folder
image

Code inside Script

local itemname = "key1"


local item = game.ServerStorage.Items:findFirstChild(tostring(itemname))
local trigger = script.Parent

enabled = true

function onClick(plyr)

	if plyr.Backpack:findFirstChild(tostring(itemname)) == nil and enabled == true then

		enabled = false



		local itemclone = item:clone()
		itemclone.Parent = plyr.Backpack

		wait(2)

		enabled = true


	end
end


script.Parent.ProximityPrompt.Triggered:Connect(onClick)

Tool in a ServerStorage folder
image

What could be causing this bug? I tried looking this up but didn’t really get any helpful results. I didn’t know this was happening until a tester spam picked up the tool while equipping it. I’d appreciate some help!!

1 Like

you probably have to check if the gear is on the player as well since it happens only when you equip the gear

Once you equip the tool, the tool gets moved from under Backpack to under Character model. You will need to check inside the Character model to check if player has equipped tool or not

You should also check if the tool is inside the character. This occurs when the player equips the tool.

I’m going to make some edits to your code to show you some good practice on things such not using FindFirstChild when the tool is supposed to be there. Also don’t use tostring when you don’t need it.

local itemname = "key1"

local item = game.ServerStorage.Items[itemname]
local trigger = script.Parent

local enabled = true

local function onClick(plyr)

	if plyr.Backpack:FindFirstChild(itemname) == nil and plyr.Character:FindFirstChild(itemname) == nil and enabled == true then

		enabled = false


		local itemclone = item:Clone()
		itemclone.Parent = plyr.Backpack

		wait(2)

		enabled = true


	end
end


script.Parent.ProximityPrompt.Triggered:Connect(onClick)

I hope this works for you <3

Oh I didn’t make the script, a good friend of mine gave it to me a while back.

Btw, I tried the code and a few things happened. The script didn’t work now but when I checked in the Output to see what it could be I seen this

When I removed the “)” from

local item = game.ServerStorage.Items[itemname])

The script worked again but it was still duplicating the tools like how it was in the clip.

Also a bit of Line 10 got underlined.


Did I miss a step?

No this is actually my fault, sorry.

local itemname = "key1"

local item = game.ServerStorage.Items[itemname]
local trigger = script.Parent

local enabled = true

local function onClick(plyr)

	if plyr.Backpack:FindFirstChild(itemname) == nil and plyr.Character:FindFirstChild(itemname) == nil and enabled == true then

		enabled = false


		local itemclone = item:Clone()
		itemclone.Parent = plyr.Backpack

		wait(2)

		enabled = true


	end
end


script.Parent.ProximityPrompt.Triggered:Connect(onClick)

Forgot to make it check in the character. Instead I copy pasted.

2 Likes
local part = script.Parent
local prompt = part.ProximityPrompt

local server = game:GetService("ServerStorage")
local items = server.Items
local item = items.key1

local debounce = false

local function onClick(player)
	if debounce then return end
	
	if player.Character:FindFirstChild(item.Name) or player.Backpack:FindFirstChild(item.Name) then
		return
	end
	
	debounce = true
	local itemclone = item:Clone()
	itemclone.Parent = player.Backpack
	task.wait(2)
	debounce = false
end

prompt.Triggered:Connect(onClick)