How do you get a tool while you are inside a part but once you leave the part it removes the tool?

So basically how do I make a script for a part that gives a tool and once you touch it again you lose the tool?

1 Like

So connect a function to the OnTouched mechanic when the player touches it. Get the Player by using GetPlayerFromCharacter from a returned arguments parent (hit.Parent for example). Check using FindFirstChild() if player has the tool in their Backpack or Character, delete it. Clone the Tool to Player’s Backpack if not the tool is not found.

1 Like

Ok, but how would I put that in a script? I’m basically a beginner scripter and can’t do really tough things.

First off put your tool in Replicated Storage:
image

Then, add a script into ServerScriptService:
image

In the script, paste this and change the variables to fir your situation:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Tool = ReplicatedStorage.Tool --//Reference your tool correctly
local Part = workspace.Part --//Reference your part to be touched correctly

--//Controls
local Debounce = false
local Cooldown_Time = 1

--//Functions
Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if Player then
		if Debounce == false then
			Debounce = true
			
			local SearchResult = Player.Backpack:FindFirstChild(Tool.Name)
			
			if SearchResult then
				SearchResult:Destroy()
			else
				local Clone = Tool:Clone()
				Clone.Name = Tool.Name
				Clone.Parent = Player.Backpack
			end
			
			task.wait(Cooldown_Time)
			Debounce = false
		end
	end
end)
1 Like