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

Why does the following script give me the error specified?

Asked by 4 years ago
Edited 4 years ago

My current modulescript is here:

function gamelib.smoothTransform(object, xIncrement, yIncrement, zIncrement, smoothingScale, delayS, xActive, yActive, zActive)
    local newX = xIncrement / smoothingScale
    local newY = xIncrement / smoothingScale
    local newZ = xIncrement / smoothingScale
end

The script which calls this is here:

GL:smoothTransform(workspace.Part, 10, 10, 10, 0.2, 0.3, true, true, true)

But, the error is here: ServerScriptService.GameLib:22: attempt to perform arithmetic on local 'xIncrement' (a userdata value)

I am curious as to why this is occuring. I don't think it is a userdata value with my understanding of what a userdata value is. Please, if you can, correct me and tell me where I'm wrong, but don't directly feed me the code.

Thanks!

1 answer

Log in to vote
1
Answered by
y3_th 176
4 years ago

Explanation

The reason why your script isn't working is because you're trying to call the function as a method, which automatically sets the first parameter to self.

This means that methods like :Destroy can also be written as .Destroy as long as you pass in the object you want to destroy first.

--GlobalScript in ServerScriptService
workspace.foo:Destroy() --using method
workspace.bar.Destroy(workspace.bar) --using function

However, this can bring up some problems when you define a function and attempt to call it as a method. Instead of passing in the expected first argument, it ends up passing itself as the first argument, which can cause a problem.

local foo = {}
foo.bar = function(baz, qux)
    return baz + qux
end

print(foo.bar(1, 2)) --this will output 3
print(foo:bar(1, 2)) --this will end up throwing an error because it's trying to add a table (foo) to a number (baz) because it ends up passing the table as the first argument

What you ended up doing was calling a function as a method, which caused the function to pass the first argument as your module and the second argument as your object, which causes an error because you're performing arithmetic on your second argument.

Basically, just change the colon to a dot. Please read the articles below, the wiki editors are much better at explaining things that I am.

Resources

Methods

Functions

0
Thank you for the answer! This fixed it all. greeenblox 28 — 4y
Ad

Answer this question