Something is wrong with my script. When I step on my part I dont take damage.
local CollectionService = game:GetService("CollectionService")
local Obstacles = CollectionService:GetTagged("obstacles")
local damageAmount = 100
for _, obstacle in ipairs(Obstacles) do
obstacle.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(damageAmount)
end
end)
end
The issue might be caused by the connection of the Touched event using the deprecated connect function. Use Connect with the capital āCā instead.
local CollectionService = game:GetService("CollectionService")
local Obstacles = CollectionService:GetTagged("obstacles")
local damageAmount = 100
for _, obstacle in ipairs(Obstacles) do
obstacle.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(damageAmount)
end
end)
end
If you are still having issues, Add a debounce variable and use it to control when the player takes damage:
local CollectionService = game:GetService("CollectionService")
local Obstacles = CollectionService:GetTagged("obstacles")
local damageAmount = 100
local function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(damageAmount)
end
end
for _, obstacle in ipairs(Obstacles) do
local debounce = false
obstacle.Touched:Connect(function(hit)
if not debounce then
debounce = true
onTouched(hit)
wait(1) -- Adjust the wait time as needed to control the debounce
debounce = false
end
end)
end
It will prevent multiple damage instances from occurring too quickly if you lower the damage amount.