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)
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.
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
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