I need some help with the interaction system

The current problem I am facing right now is… How exactly can I get a tool to delete upon equipping it and placing it onto the hitbox of the item? I am not too good at coding so don’t expect the following code to be too confusing.

The script I am currently using:


I am not 100% sure what’s causing an issue because there’s some sort of dupe glitch where I can place down two bags instead of what is intended as one.

The props that give you the actual item upon picking one up

The forcefield material bags represent the hitboxes.

So if anyone has a proper solution, I would like to try out any solutions you guys have :grinning:

Your building skills: :grinning::+1:
Your scripting skills: :scream: :hocho:

here’s a more effective and clean hitbox:

--LOCALSCRIPT IN STARTERPLAYER.STARTERCHARACTERSCRIPTS
local contextActionService = game:GetService("ContextActionService")
local runService = game:GetService("RunService")


local target = workspace:WaitForChild("target")


local currentTool


local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()

local distanceDetection = 5

--epickly detect tool
contextActionService.LocalToolEquipped:Connect(function(toolEquipped)
	currentTool = toolEquipped
end)
contextActionService.LocalToolUnequipped:Connect(function()
	currentTool = nil
end)

--Checks every frame
runService.Stepped:Connect(function()
	--get distance
	local relativeVector = target.Position - chr.PrimaryPart.Position
	local distance = relativeVector.Magnitude
	
	if distance <= 5  and currentTool then
		--Place the thing
		local clone: Model = currentTool:Clone()
		
		clone.Handle.Anchored = true
		
		clone.Parent = workspace
		clone:PivotTo(target.CFrame)
		
		--destroy the player's tool
		currentTool:Destroy()
		currentTool = nil
		
	end
	
end)

I know I shouldn’t be writing code for free, but I am still practicing
Hope this helps, bye

I am kind of lost with the code right now just saying. I am not too sure where to put the words: “bag” (this is the tool) and “Bags” (this is the prop)

Mind clarifying?

Edit: the “local currentTool”

and the
“if distance <= 5 and currentTool then
–Place the thing
local clone: Model = currentTool:Clone()”

are the ones I don’t really know

I am kind of lost with the code right now just saying. I am not too sure where to put the words: “bag” (this is the tool) and “Bags” (this is the prop)

You can use currentTool.Name to check if it’s a bag or not.
For the “bags” prop, you can simply use Instance:SetAttribute(key, value) to show how many bags.
Or alternatively, you can use an IntValue Instance

Edit: the “local currentTool”

this piece of code allows me to access currentTool anywhere in the script, like a global variable

“if distance <= 5 and currentTool then

I made a mistake on this part, I meant to write “if distance <= distanceDetection and currentTool then"
the “and currentTool” is for checking if the player has the tool

local clone: Model = currentTool:Clone()”

the :Clone is very useful for copying and pasting stuff. It can be useful for car spawners, etc.

Also, the “: Model” serves no purpose. It only provides autofill, so that way I can see all the properties and methods that belongs to “Model.” It can be anything, instances, animations, etc.

you should make edits to the code to make it fit your game.
Also note, that if you use a localscript, the changes to the workspace or game won’t be seen by other players. You can only make changes through a remoteEvent, and have a script listen to it