Unexpected attempt to set a script's parent to NULL

I ran into this error while trying to make a status gui.

The code in question is this:

StatGiver

-- This script gives the values parented to it to the Character's humanoid, and destroys itself after

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")

for _,Value in ipairs(script:GetChildren()) do
	Value.Parent = Humanoid
	print(Value.Name .. " loaded, " .. Value.Parent.Name)
end

print("Stats loaded in Humanoid")

script:Destroy()

Does anyone know how to avoid it?

You have a script trying to destroy itself. My best guess is that thread is killed before :Destroy() finishes.

Either remove the final line or have another script handle destroying the script.

1 Like