Give tool when in area remove when out of area

Hey so I am trying to do a script where when you are in the area of a part you will equip a sword and when you exit it will unequip and remove from your backpack.

However I am not a scripter and am struggling a lot…

What I have:

local tool = game.ServerStorage.ClassicSword
local klone = tool:clone()
script.Parent.Touched:connect (function(plr)
if klone.Parent ~= plr.Backpack then
klone.Parent = plr.Backpack
else
end
end)

script.Parent.TouchEnded:Connect()
tool:destroy()
	
end

-- I think its completely wrong? as it doesn't work.


Essentially when you are in this red area


it equips sword is there an easier way to do it
than what i have in mind?

Region3 can solve your problem.

EDIT: here’s a document about it.

1 Like

Yea thanks for the help however im unsure how to script really I thought this would be simple like ontouch it equips when your not touching it unequips? or would that not work on a part with cancollide off…

This involves Touched events! There’s a tutorial about it on YT.

You’re trying to… uh…

  1. klone is cloned outside .Touched event, which means only 1 sword will be cloned during run time
  2. function(plr) does not give you the player, but it gives you the part that touches the script.Parent.
  3. Instead of using TouchEnded, make 2 “doors”, one for when you enter the arena, the other for when you leave the arena (make them one-way, like maybe a hole for the enter door, and a normal passage for the second door)
local tool = game.ServerStorage.ClassicSword
local klone = tool:clone()
script.Parent.Touched:connect (function(partThatTouched)
      local player = game.Players:FindFirstChild(partThatTouched.Parent.Name) -- for when we are not sure if the instance we want exists
      if player ~= nil and klone.Parent ~= player.Backpack then
            klone.Parent = player.Backpack
     end
end)


end

and for the exit “door”

script.Parent.Touched:Connect(function(hit)
local player = game.Players:FindFirstChild(partThatTouched.Parent.Name) -- again, we try to get the player
local tool -- we just declare so we can use it on the 2nd if statement
if hit.Parent:FindFirstChild("ClassicSword") then
     tool = hit.Parent:FindFirstChild("ClassicSword") 
elseif player.Backpack:FindFirstChild("ClassicSword") then
     tool = player.Backpack:FindFirstChild("ClassicSword")
end

if tool ~= nil then
     tool:Destroy()
end
end)

Ask me questions I gotta go real quick, also the modified 1st code is kinda wrong

1 Like

This might work, but Region3 saves up a lot of spaces.


The first script works like a treat this one is there meant to be a varible for ‘partThatTouched’

image

Fixed it thanks it was to do with the function

Oh yeah there’s a typo there. glad you managed to fix it :wink:

Also for some reason when I die or unequip and try to get it again it prints this do you know how to sort that?

I feel you shouldn’t use touched as it isn’t reliable and consistent. Rather there are much better alternatives such as region3 or this resource provided by @ForeverHD. This is a much better alternative as it uses recasting and regions, instead.

Here is something I put together a while back tweaking the resources’ code after someone asked the same question you asked. Feel free to look into it. However, if you want to achieve something else such as touching part gives tool and vice verca, then disregard my response.

SwordArea.rbxl (28.1 KB)

Hmm let me try my own script in studio. brb.

Ok, the problem was that
klone is a ClassicSword cloned when the script runs
basically it’s not cloned in the function so there’s only 1 sword
when it gets destroyed, the parent property is locked, hence the error

So, the solution is to make the klone inside the function, but now a new problem arises:

  1. If you walk into the door it will make 20+ copies because it registers 20+ touched events
  2. The exit door will only delete 1 sword so we must only make 1 copy

To overcome this, we will do checks if a ClassicSword is in the backpack or is being equipped, and when we detect it, we will stop the function using this:

return

when you learn further, return actually can make a function give you any data, like how our backpack:FindFirstChild(“ClassicSword”) gives us nil or ClassicSword.

local tool = game.ServerStorage.ClassicSword

script.Parent.Touched:connect (function(hit)
	local klone = tool:clone()
	local player = game.Players:FindFirstChild(hit.Parent.Name) -- for when we are not sure if the instance we want exists
	if player ~= nil then
		if player.Backpack:FindFirstChild("ClassicSword") or hit.Parent:FindFirstChild("ClassicSword") then
			return -- return makes the function we run, stop
		end
	end
	
	if player ~= nil and klone.Parent ~= player.Backpack then
		klone.Parent = player.Backpack
		
	end
end)

7 Likes

Great thank you your a life saverr lol

You’re welcome. :slight_smile:

2 Likes