TouchEnded fires whenever player is inside of the BasePart

Hello! I have a problem. The TouchEnded event keeps firing.


The Code:

local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage.Sword

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not plr:GetAttribute("HasSword") then
			plr:SetAttribute("HasSword", true)
			local c = tool:Clone()
			c.Parent = plr.Backpack
			hit.Parent.Humanoid:EquipTool(c)
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Backpack:FindFirstChild("Sword") then
			hit.Parent.Humanoid:UnequipTools()
			plr.Backpack.Sword:Destroy()
			plr:SetAttribute("HasSword", false)
		end
	end
end)

If you can help, please let me know! Thanks, WE

This is intended. Touched(edEnded) is a relatively unpredictable event. Consider using Zone+ by ForeverHD for efficient, reliable, and predictable Zone detection. I use it for my sword fighting game and it works perfectly.

1 Like

This time, the player’s sword will not disappear. Am I doing something wrong?

local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage.Sword
local ZoneService = require(game:GetService("ServerScriptService"):WaitForChild("Zone"))

local bounds = ZoneService.new(script.Parent)

bounds.playerEntered:Connect(function(plr)
	--if hit.Parent:FindFirstChildOfClass("Humanoid") then
		--local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not plr:GetAttribute("HasSword") then
			plr:SetAttribute("HasSword", true)
			local c = tool:Clone()
			c.Parent = plr.Backpack
			plr.Character.Humanoid:EquipTool(c)
		end
	--end
end)

bounds.playerExited:Connect(function(plr)
	--if hit.Parent:FindFirstChildOfClass("Humanoid") then
		--local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Backpack:FindFirstChild("Sword") then
			plr.Character.Humanoid:UnequipTools()
			plr.Backpack.Sword:Destroy()
			plr:SetAttribute("HasSword", false)
		end
	--end
end)

This should work, if it doesn’t then I can fix it:


local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage.Sword

local part = script.Parent

local x = part.Position + Vector3.new(part.Size.X/2, part.Size.Y/2, part.Size.Z/2)
local y = part.Position - Vector3.new(part.Size.X/2, part.Size.Y/2, part.Size.Z/2)

local region = Region3.new(x, y)

local players = {}
while wait(1) do
	local current = {}
	for _,Part in pairs(game.Workspace:FindPartsInRegion3(region,nil,math.huge)) do
		if game.Players:GetPlayerFromCharacter(Part.Parent) and not table.find(current, game.Players:GetPlayerFromCharacter(Part.Parent)) then
			table.insert(current, game.Players:GetPlayerFromCharacter(Part.Parent))
		end
	end
	for i, plr in pairs(current) do
		if not table.find(players, plr) then
			if not plr:GetAttribute("HasSword") then
				plr:SetAttribute("HasSword", true)
				local c = tool:Clone()
				c.Parent = plr.Backpack
				plr.Character.Humanoid:EquipTool(c)
			end
		else
			table.remove(players, plr)
		end
	end
	for i, plr in pairs(players) do
		if plr.Backpack:FindFirstChild("Sword") then
			plr.Character.Humanoid:UnequipTools()
			plr.Backpack.Sword:Destroy()
			plr:SetAttribute("HasSword", false)
		end
	end
	players = current
end

It didn’t work.

There was an error
image

Also, I kind of want to use ZonePlus because I use other API’s, of ForeverHD, such as TopBar Plus

1 Like

I had a simple typo but I just fixed it, I’m going to test it on my end and I’ll get back to you in a couple minutes after I have fixed everything.

1 Like

Turns out, the issue to this is because I modified some scripts affecting the players backpack.

I got the script to work. Also I gtg but if you have any questions I can help you in the morning.
https://gyazo.com/7858ebac1e20828c89b485a05a10653d

local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage.Sword

local part = script.Parent

local x = part.Position + Vector3.new(part.Size.X/2, part.Size.Y/2, part.Size.Z/2)
local y = part.Position - Vector3.new(part.Size.X/2, part.Size.Y/2, part.Size.Z/2)

local region = Region3.new(y, x)

local players = {}
while wait(0.5) do
	local current = {}
	for _,Part in pairs(game.Workspace:FindPartsInRegion3(region)) do
		print(Part.Name)
		if game.Players:GetPlayerFromCharacter(Part.Parent) and not table.find(current, game.Players:GetPlayerFromCharacter(Part.Parent)) then
			table.insert(current, game.Players:GetPlayerFromCharacter(Part.Parent))
		end
	end
	print(current)
	for i, plr in pairs(current) do
		if not table.find(players, plr) then
			print("equip")
				local c = tool:Clone()
				c.Parent = plr.Backpack
				plr.Character.Humanoid:EquipTool(c)
		else
			table.remove(players, table.find(players, plr))
		end
	end
	for i, plr in pairs(players) do
		print("Test")
		if plr.Backpack:FindFirstChild("Sword") or plr.Character:FindFirstChild("Sword") then
			plr.Character.Humanoid:UnequipTools()
			plr.Backpack.Sword:Destroy()
		end
	end
	players = current
end