Wwhy is it printing many times on the console? I just want that when the player touches the part, the sript prints “Touched”, when it leaves the script prints “TouchedEnd”, not spam the console
SCRIPT:
Debounce = false
script.Parent.Touched:Connect(function(Hit)
if not Debounce then
print("Touched")
Debounce = true
end
end)
script.Parent.TouchEnded:Connect(function(Hit)
if Debounce then
print("TouchedEnd")
Debounce = false
end
end)
Debounce = false
script.Parent.Touched:Connect(function(Hit)
if not Debounce then
print("Touched")
Debounce = true
end
end)
script.Parent.TouchEnded:Connect(function(Hit)
if Debounce then
print("TouchedEnd")
wait(1)
Debounce = false
end
end)
Touched and TouchEnded aren’t the best way to go about this. As you’ve discovered they can get really messy to deal with so I would recommend trying other more ideal methods. Since Touched events shouldn’t be the go to method of identifying if a player is within a certain region of the world.
If you could explain what your end goal is I could go about guiding you towards a better method.
I intend to make a damage script with speed reduction, and animation when the player takes damage with TakeDamage (), I already did one, but I lost and I don’t know how
My assumption, is you could use a Region3 and every X seconds, use WorldRoot:FindPartsInRegion3 to find which players are inside the box to deal damage too.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ZonePart = script.Parent
local Touching = false
local function isInsideBrick(Pos, Part)
local v3 = Part.CFrame:PointToObjectSpace(Pos)
return (math.abs(v3.X) <= Part.Size.X / 2)
and (math.abs(v3.Y) <= Part.Size.Y / 2)
and (math.abs(v3.Z) <= Part.Size.Z / 2)
end
local function getPlayersInsideZone(Zone)
List = {}
for _, Player in pairs(Players:GetPlayers()) do
if (Player.Character) then
local RootPart = Player.Character:FindFirstChild("HumanoidRootPart")
if (RootPart and isInsideBrick(RootPart.Position, Zone)) then
table.insert(List, Player)
end
end
end
return List
end
RunService.Heartbeat:Connect(function()
if (#getPlayersInsideZone(ZonePart) > 0) then
if not Touching then
Touching = true
CurrentPlayer = List[1]
print(CurrentPlayer.Name .. " has entered")
end
else
if Touching then
Touching = false
print(CurrentPlayer.Name .. " has left")
end
end
end)