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
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
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!!
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)
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.
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)