So I have this script where if a part was touched and player clicked on the screen then put it in the player’s inventory folder
local tracker = script.Parent
tracker.Parent.Enabled = true
local tools = workspace.Tools
for _,v in tools:GetChildren() do
tracker.Activated:Connect(function()
v:WaitForChild('Handle').Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
v.Parent = hit.Parent:WaitForChild('Inventory')
v.Handle.Part.CanCollide = false
v:WaitForChild('BillboardGui').Enabled = false
end
end)
end)
end
and the script basically skips over the first one, if the tracked isn’t activated it doesn’t care, I understand why this is happening but is there a way around this?
This script is a part of a pick up tool function I made, if the player clicked anywhere on the screen and also is touching said tool then put it in player.character.inventory.
Are you working with a custom inventory system? Character.Inventory is not a default feature as far as I know. Also I’m a bit confused here. The tracker variable is a button? If so, why dont you just use UserInputService to detect clicks. Something like this should work:
game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
—Code here
end
end)
I am working with a custom tool system, the tracker is a gui button since at the start of the project I needed and still need some server scripts to detect user mouse click so I just used that. My method and MouseButton1Down are different. This could work, I will test it right now.
I think this is happening because your .Touched for the tool proximity detection can still go off even when it is in a players inventory. One thing you could do is set the Handle’s CanTouch to false once the tool is picked up. That way, the handle won’t detect the player who is holding it.
.Touched will not work here since it’s an event. Once you connect the event it will listen for it forever or until the connection is disconnected. It won’t work as an if statement thing. Apart from that you’re constantly creating new connection each time Tracker get’s activated which is a memory leak
What I’d do is use workspace:GetPartsInPart() or workspace:GetPartBoundsInBox(). Alternatively you can use the distance between HumanoidRootPart position and the tool’s handle which would probably be less expensive if that matters.
Also I’m not sure why you’re making a new connection for each tool? Just loop through each tool inside of the connection. This not only makes sure that if you add a new tool it’s going to be working correctly but also just looks better overall
Here’s how the entire script would look like with my changes using the :GetPartsInPart method
local tracker = script.Parent
tracker.Parent.Enabled = true
local tools = workspace.Tools
local overlap_params = OverlapParams.new() -- Change these to make them work best for what u need
tracker.Activated:Connect(function()
for _,v in tools:GetChildren() do
local handle = v:FindFirstChild("Handle")
if handle == nil then
continue
end
local parts = workspace:GetPartsInPart(v:FindFirstChild("Handle"), overlap_params)
if parts == nil then
continue
end
for index, part in ipairs(parts) do
local character = part.Parent
local humanoid = character:FindFirstChild("Humanoid")
local inventory = character:FindFirstChild("Inventory")
if humanoid and inventory then
v.Parent = inventory
handle.Part.CanCollide = false
v.BillboardGui.Enabled = false
end
end
end
end)