I attempted to make a code that if someone is detected within the box, they won’t be detected again unless they leave and reenter the hitbox. However, this is not working.
The code I’m talking about:
local hitbox = script.Parent
local char = hitbox.Parent.Parent
hitbox.Touched:Connect(function(hit)
if script.AlreadyChecked.Value ~= true then
if hit:IsA("Part") or hit:IsA("MeshPart") then
if hit.Parent ~= char and hit.Parent:findFirstChild("Humanoid") then
print(hit.Parent.Name)
local name = hit.Parent.Name
script.Found.Value=true
if hit.Parent.Name == nil then
script.Found.Value=false
elseif hit.Parent.Name == name then
script.Found.Value=true
end
end
end
elseif script.AlreadyChecked.Value == true then
wait(2)
end
end)
-- function onTouched(hit)
-- if hit:IsA("Part") or hit:IsA("MeshPart") then
-- if hit.Parent ~= char and hit.Parent:findFirstChild("Humanoid") then
-- print("its not you and not a wall but rather a guy")
-- end
--end
--end
You could have it only detect a certain part of a player, as usually it will detect left arm, then right arm all as separate touches etc. Instead, you could have a
This should hopefully work, let me know if there are any issues with it
local hitbox = script.Parent
local char = hitbox.Parent.Parent
local debounce = nil
hitbox.Touched:Connect(function(otherPart)
if otherPart.Name ~= "HumanoidRootPart" then
return
end
if otherPart.Parent == char then
return
end
if debounce then
return
end
debounce = true
--[[
The code above is to prevent messy "nested" code (if statements in if statements in if statements etc)
It checks if the part touching is named "HumanoidRootPart" (central part of the character)
If it's a HumanoidRootPart, it makes sure that it's another player, as this code seems to be a character hitbox, so you'll want to avoid hitting yourself.
If it isn't, it checks for if the "debounce" variable is true, this is to ensure it only fires once.
It then sets debounce to true so that it cannot fire again.
]]--
--Do stuff
end)
hitbox.TouchEnded:Connect(function(otherPart)
if otherPart.Name ~= "HumanoidRootPart" then
return
end
if otherPart.Parent == char then
return
end
debounce = false
--[[
There's no need to check for debounce, because for a touch to end, a touch need to begin.
And every time a touch begins with a HumanoidRootPart, debounce is always going to be true.
Of course, we still need to check if the HumanoidRootPart belongs to the current character because otherwise you'll be permanently hittable
Assuming the hitbox isn't tied to the HumanoidRootPart.
]]--
--Do stuff
end)