Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Object expected, got number when using spawn?

Asked by 5 years ago

i am trying to make a function that "dusts" a player (think infinity war) when called on a player, and it has a function inside it that dusts a single part, which is used in a for loop to dust every part, i cant dust them without using spawn() or else the dusting will take very long, the DustPart(Part) function is being called via spawn(DustPart)(v), but DustPart thinks v is a number when it an instance?

function DustPlayer(Victim)
    local PartToDust
    local function DustPart(Part)
        local GrainyParticles = Instance.new("ParticleEmitter")
        GrainyParticles.EmissionDirection = Enum.NormalId.Left
        GrainyParticles.Lifetime = NumberRange.new(1)
        GrainyParticles.Rate = 50
        GrainyParticles.Rotation = NumberRange.new(-360,360)
        GrainyParticles.RotSpeed = NumberRange.new(-360,360)
        GrainyParticles.Speed = NumberRange.new(5)
        GrainyParticles.Size = NumberSequence.new(2)
        GrainyParticles.Acceleration = Vector3.new(-15,10,0)
        GrainyParticles.Color = ColorSequence.new(Color3.fromRGB(111,98,92))
        GrainyParticles.LightInfluence = 1
        GrainyParticles.Transparency = NumberSequence.new({
            NumberSequenceKeypoint.new(0,1,0),
            NumberSequenceKeypoint.new(0.15,0.1,0),
            NumberSequenceKeypoint.new(0.5,0.7,0),
            NumberSequenceKeypoint.new(1,1,0)
        })
        GrainyParticles.ZOffset = 0.1
        GrainyParticles.Texture = "rbxassetid://363275192"
        GrainyParticles.Parent = Part
        local SoftParticles = Instance.new("ParticleEmitter")
        SoftParticles.EmissionDirection = Enum.NormalId.Left
        SoftParticles.Lifetime = NumberRange.new(1)
        SoftParticles.Rate = 50
        SoftParticles.Rotation = NumberRange.new(-360,360)
        SoftParticles.RotSpeed = NumberRange.new(-360,360)
        SoftParticles.Speed = NumberRange.new(5)
        SoftParticles.Size = NumberSequence.new(2)
        SoftParticles.Acceleration = Vector3.new(-15,10,0)
        SoftParticles.Color = ColorSequence.new(Color3.fromRGB(158,158,158))
        SoftParticles.LightInfluence = 1
        SoftParticles.Transparency = NumberSequence.new({
            NumberSequenceKeypoint.new(0,1,0),
            NumberSequenceKeypoint.new(0.15,0.1,0),
            NumberSequenceKeypoint.new(0.5,0.7,0),
            NumberSequenceKeypoint.new(1,1,0)
        })
        SoftParticles.Texture = "rbxassetid://3176154112"
        SoftParticles.Parent = Part
        local Tween = TService:Create(Part,DustInfo,DustDict)
        Tween:Play()
        wait(2)
        Part.Transparency = 1
        Part.CanCollide = false
        SoftParticles.Enabled = false
        GrainyParticles.Enabled = false
        wait(1)
    end
    local Sound = Instance.new("Sound")
    Sound.PlayOnRemove = true
    Sound.Volume = 5
    Sound.SoundId = Sounds.Dustings[math.random(1,#Sounds.Dustings)]
    Sound.Parent = Victim.Character.Head
    Sound:Destroy()
    for k,v in pairs(Victim.Character:GetChildren()) do
        if v:IsA("BasePart") then
            spawn(DustPart)(v)
            if v.Name == "Head" then
                v:FindFirstChildOfClass("Decal"):Destroy()
            end
            wait(0.5)
        end
        if v:IsA("Accoutrement") then
            v.Handle:FindFirstChildOfClass("SpecialMesh").TextureId = ""
            DustPart(v.Handle)
            wait(0.5)
        end
        wait()
    end
end

1 answer

Log in to vote
1
Answered by
Ankur_007 290 Moderation Voter
5 years ago

The problem in your script is the way you call your DustPart function, the format spawn(DustPart)(v). v is actually never passed to the DustPart function and instead, the first parameter of spawn- namely the delta time - is passed by spawn. You must use anonymous functions to achieve what you need.

Demonstration

To check the behaviour of spawn() we run a test function without any anonymous functions:

local function test(x) 
    wait(3) 
    print(x) 
end 

spawn(test)("Waited 3 seconds!")

print("Beginning")

The output we observe is:

03:00:55.491 - [string "local function test(x, y) ..."]:6: attempt to call a nil value
-- 3 seconds later
0.031062014207009

The number in the latter print is the delta time, or the time passed since when spawn was called to when the function in it was invoked. A better look at this is through the following script:

local function test(x) 
    print(tick())
    wait(3) 
    print(x) 
end 

print(tick())
spawn(test)

print("Beginning")

We get the values 1560049557.6195 and 1560049557.5887 from our print(tick())s, on subtracting which we get 0.030799865722656, very close to our value of x: 0.030203970198272. Similarly there is a second value passed by spawn, which I don't know well enough to explain. To quote Seranok from a post:

Spawn calls the function with two arguments: the first being the amount of time which elapsed from when spawn was called to when the function was invoked, and the second being equivalent to elapsedTime() or roughly how long the engine has been running.

The Solution

The fix to this problem is an easy one once you know what's going wrong, just use an anonymous function while calling the DustPart function as such:

spawn(function()
    DustPart(v)
end)

That's it from me! I hope this solved your problem, feel free to ask any questions or point out any mistakes in the comments.

0
IT WORKED! thanks for your help! fanofpixels 718 — 5y
Ad

Answer this question