Hi there, so I am having trouble with my touch to damage script. What it is supposed to do is that when a Humanoid touches it, it does some damage every second. But instead, it’s waiting (for 1 second) then does lots of damage even though the take damage is set to 1. Sorry if I did not make sense but it’s not supposed to repeat the take damage then wait. It’s supposed to - 1 health then wait.
function onTouched(hit)
if not hit or not hit.Parent or not game.Players:GetPlayerFromCharacter(hit.Parent) then return end
local human = hit.Parent:findFirstChild("Humanoid")
if human and human:IsA("Humanoid") then
human.Health = human.Health-1
wait(5)
end
end
script.Parent.Touched:connect(onTouched)
A few things are wrong, first off there’s no debounce so it will constantly hit. Also you are waiting after doing damage which will do basically nothing for you. And finally if you want to do damage over time you should use a for loop. Below is an example of what you should add into it (Debounce + loop for damage)
local hitTable = {}
function onTouched(hit)
if not hit or not hit.Parent or not game.Players:GetPlayerFromCharacter(hit.Parent) then return end
local human = hit.Parent:findFirstChild("Humanoid")
if human and human:IsA("Humanoid") and not hitTable[human.Parent] then
hitTable[human.Parent] = true -- a debounce to prevent multiple hits
for i = 1, 5 do -- loops 5 times
human.Health -= 1 -- -= 1 is the same as = human.Health - 1
wait(1)
end
hitTable[human.Parent] = false
end
end
script.Parent.Touched:Connect(onTouched) -- changed :connect to :Connect to be more consistent with current roblox coding practices
Thank you very much. This works!