Instance.new() not working? bug?

In my combat script, I have a function that when called, spawns a Instance.new() on the specified player’s humanoid:

local function SpawnInstance(a,b,c,d)												
	local v = Instance.new(a,b)	
	v.Name = c									
	v.Value = true	
	delay(d,function()						
		v:Destroy()									
	end)			
end
SpawnInstance("BoolValue",Humanoid,"Name",1)

The problem is, it will only spawn on the humanoid if the script goes like this.

local function SpawnInstance(a,b,c,d)												
	local v = Instance.new(a,b)	
	v.Name = c									
	v.Value = true	
	wait(d)					
	v:Destroy()											
end
SpawnInstance("BoolValue",Humanoid,"Name",1)

As you may notice, the only difference is in how the script waits before destroying the instance. I am utterly bamboozled. Any ideas?

You are passing the humanoid as a parent on both examples. Why you are expecting a different result?

The problem is, the results ARE different. Did you read “The problem is, it will only spawn on the humanoid if the script goes like this.”

Perhaps the DebrisService would help here:

local DS: DebrisService = game:GetService("Debris")

function spawn(obj_type, par, name, duration)
    local obj: obj_type = Instance.new(obj_type)
    obj.Name = name
    obj.Parent = par
    DS:AddItem(obj, duration)
end

Yes, sorry I forgot to mention. That’s what I originally had it as, it didn’t work. I just tried it again after reading your post, still doesn’t spawn the instance on the Humanoid.

I’ve tried both of your snippets, and they work fine. Are you sure it’s this code that’s causing the issue?

Yeah both works same for me as expected. Your problem is not related to these functions.

You are right. The code is not the problem. I replicated it elsewhere in the script and it worked. Super weird though, it doesn’t throw any errors.


print("1")
SpawnInstance("BoolValue",Humanoid,"Slicing",SliceLength+.01)
print("2)

it also prints “1” and “2”. All the conditions required to run this code are met, and those same conditions that run a bunch of other code work just fine. I’ll keep digging.