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?

01function DustPlayer(Victim)
02    local PartToDust
03    local function DustPart(Part)
04        local GrainyParticles = Instance.new("ParticleEmitter")
05        GrainyParticles.EmissionDirection = Enum.NormalId.Left
06        GrainyParticles.Lifetime = NumberRange.new(1)
07        GrainyParticles.Rate = 50
08        GrainyParticles.Rotation = NumberRange.new(-360,360)
09        GrainyParticles.RotSpeed = NumberRange.new(-360,360)
10        GrainyParticles.Speed = NumberRange.new(5)
11        GrainyParticles.Size = NumberSequence.new(2)
12        GrainyParticles.Acceleration = Vector3.new(-15,10,0)
13        GrainyParticles.Color = ColorSequence.new(Color3.fromRGB(111,98,92))
14        GrainyParticles.LightInfluence = 1
15        GrainyParticles.Transparency = NumberSequence.new({
View all 73 lines...

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:

1local function test(x)
2    wait(3)
3    print(x)
4end
5 
6spawn(test)("Waited 3 seconds!")
7 
8print("Beginning")

The output we observe is:

103:00:55.491 - [string "local function test(x, y) ..."]:6: attempt to call a nil value
2-- 3 seconds later
30.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:

01local function test(x)
02    print(tick())
03    wait(3)
04    print(x)
05end
06 
07print(tick())
08spawn(test)
09 
10print("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:

1spawn(function()
2    DustPart(v)
3end)

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