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.
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…
klone is cloned outside .Touched event, which means only 1 sword will be cloned during run time
function(plr) does not give you the player, but it gives you the part that touches the script.Parent.
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
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.
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:
If you walk into the door it will make 20+ copies because it registers 20+ touched events
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)