local HP = script.Parent
local healingIndex = {}
HP.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if healingIndex[player] then
return
end
local healing = coroutine.create(function()
healingIndex[player] = true
local hum = hit.Parent.Humanoid
player:GetPlayerFromCharacter(hit.Parent)
for i = 1, 5 do
hum.Health += 2
task.wait(0.5)
healingIndex[player] = nil
end
end)
coroutine.resume(healing)
print(coroutine.status(healing))
end)
What I tried: I tried experimenting with indents and added a for loop instead of ticks because I don’t know how to use them.
Why: I’m learning Coroutine
What’s wrong?: It’s printing out “dead”
Help: What am I doing wrong?
Goal: I’m making a when the HealthPart is being touched, it adds 2 HP for each 0.5s.
It should have given you an error that GetPlayerFromCharacter is not a member of Player. It should be game.Players:GetPlayerFromCharacter(hit.Parent) as it’s a member of the players service (or just remove that line altogether as it doesn’t look like you are doing anything with it)
As @7z99 said there’s a “syntax”[?] error in your code: GetPlayerFromCharacter is not a valid member of Player
You can try converting this into a regular function and check how it executes
OR you can check resume with the code:
local success, result = coroutine.resume(healing)
if not success then
print("Coroutine crashed with error:", result)
else
print("Coroutine ran successfully:", result)
end
It will output: Coroutine crashed with error: GetPlayerFromCharacter is not a valid member of Player
but the output misses the line where errors happen.
The point is that execution of coroutines hides errors.
While coroutines might look like a nice programming feature I left them because of this: it’s harder to find mistakes even simple typos.
You should consider spawning the thread as a task instead (task.spawn). It’s the same framework, but with the QoL of Roblox’s task scheduler and error handler.