How can I take damage with a tool in limited area only?

Hello fellow devs!

Now I am releasing an experience called “Samurai Era”.

As you can see the picture below, there is a region (colored with half transparent red)
where a player can fight against other players with a bamboo sword.

Because I want to make the bamboo sword take damage in this area only,
I wrote a server script just under the region part as follows:

Blockquote
local hitRegionPart = script.Parent
local filter = OverlapParams.new()
filter.FilterDescendantsInstances = {}
filter.FilterType = Enum.RaycastFilterType.Whitelist

local parts = game.Workspace:GetPartsInPart(hitRegionPart, filter)

while true do
for _, v in pairs (parts) do
if v.Name == “Blade” then
v.Touched:Connect(function(otherpart)
if otherpart.Parent:WaitForChild(“Humanoid”) then
local hum = otherpart.Parent.Humanoid
hum:TakeDamage(5)
wait(1)
end
end)
end
end
end

Blockquote

There is no error but a player never take damage from the dummy in the region.
I don’t know what is wrong.
I appreciate if someone help me.

have a look at this, this might be useful

1 Like

Thank you for your quick replay. I will definitely check the reference.
However, if possible, I want to know what is wrong with my code.

well for one, you’re running a while loop without any sort of waiting inside of it, that will lead to script timeout or whatever. second, you’re connecting a touched event to your blade each time you run this while loop IF there is a blade inside the regionpart.

1 Like

If I’m reading it correctly, you’re not filtering anything. You have a Whitelist and put nothing in the filter descendants so nothing is getting picked up by the GetPartsInParts.

You’re basically saying:

  1. you: Hey, I want you to ONLY detect when it hits these items
  2. function: Ok, what items would you like me to detect?
  3. you: These items: Filter = {}

An empty table. There are probably more errors and things missing but that was the first one I saw

2 Likes

Thank you for your reply. it is very educational. I think maybe it is better to rewrite the code with Zone. I will update if I have problems then.

Thank you for your reply. Yes, I think my current code has a lot of problems, so I think I should try Zone first and now writing the code. Thank you for your smart explanation easy to understand.

1 Like

I rewrote the code with Zone as follows:

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = workspace.SamuraiSchool.HitRegionPart
local zone = Zone.new(container)

local players = game:GetService("Players")
local weapons = game:GetService("ReplicatedStorage").Weapons
local shinai = weapons.Shinai


--local FORCE_FIELD_NAME = "SafeZoneForceField"
--local forceFieldTemplate = Instance.new("ForceField")
--forceFieldTemplate.Name = FORCE_FIELD_NAME

zone.playerEntered:Connect(function(plr)
	local char = plr.Character
	local hum = char:FindFirstChild("Huamnoid")
	local clone = shinai:Clone()
	clone.Parent = char
	--hum:EquipTool(clone)
end)

zone.playerExited:Connect(function(plr)
	local itemName = "Shinai" --Change this
	if plr.Backpack:FindFirstChild(itemName) and plr.Backpack[itemName]:IsA("Tool") then
		plr.Backpack.Shinai:Destroy()
	end --Destroy Non-Equipped tool in inventory / Backpack
	if plr.Character and plr.Character:FindFirstChild(itemName) and plr.Character[itemName]:IsA("Tool") then
		plr.Character[itemName]:Destroy()
	end --Destroy Equipped Tool
end)

This code works fine. And so I close this thread. Thank u so much for ur cooperation guys!

1 Like