I'm trying to make a sword arena script where if you are in a certain area then you get a sword and if you leave that area the sword is removed
Possible ways I could have done this:
- Check if a player is in a certain X, Y, Z
- Check if a player is in a region using region3
- Check if a player has touched a brick
I used option 3 and have the below code
opened = false
local check = game.Workspace.check
local sword = game.ReplicatedStorage.Heal
check.Touched:connect(function(hit)
if opened then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") then
char = plr.Character
if not hit.Parent:FindFirstChild("Heal") then
if not game.Players[hit.Parent.Name].Backpack:FindFirstChild(sword.Name) then
sword:Clone().Parent = hit.Parent
wait(5)
end
end
end
end
end)
function CheckIfPlayerIsInArea(check,Character)
local touching = check:GetTouchingParts()
for i=1,#touching do
if touching[i] == Character.HumanoidRootPart then
return true
end
end
return false
end
check.TouchEnded:connect(function(remove)
if remove.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(remove.Parent)
local char = plr.Character
if CheckIfPlayerIsInArea(check, char) == false then
if plr.Backpack:FindFirstChild("Heal") then
plr.Backpack.Heal:Destroy()
elseif plr.Character:FindFirstChild("Heal") then
char.Heal:Destroy()
end
end
end
end)
The problem is if the user is on the edge it spam gives them the sword making the annoying unsheath sound for swords.
I've tried adding a wait() to both remove and give functions but it didn't seem to do much.
How do I get around this as I don't want players spamming their sword and is there a better way to script this?
Thanks in advance!

1 comment
That's a good idea but the problem still persists. I think its how I set up the touch and touchended functions. If the user is on the edge its bound to just remove and add the tool. I'll try to use inequalities to see if the character is in a certain X, Y, Z instead