My code works fine, but throws an error after the coroutine dies, and I can’t figure out why.
These are the relevant parts of the script:
function tween(coin,pos)
local T = tweenservice:Create(coin, TweenInfo.new(tweenTime, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = pos, Transparency = 1}) T:Play()
T.Completed:Wait()
coin:Destroy()
end
root = script.Parent.HumanoidRootPart
root.Touched:Connect(function(touchedObject)
if touchedObject.Name == "Hitbox" then
touchedObject.CanTouch = false
touchedObject.Parent.CanCollide = false
local plr = players:GetPlayerFromCharacter(root.Parent)
plr.leaderstats.Coins.Value += touchedObject.Parent.Parent.Worth.Value
coroutine.wrap(tween(touchedObject.Parent, root.Position)) -- Erroring line here
end
end)
coroutine.wrap() return a function value that is a coroutine wrapped around your desired function, as such you must append () at the end in order to call the returned function, alternatively, you can assign the function somewhere for later use.
local routine = coroutine.wrap(print)
task.wait(1)
routine("Hello world!")