Assistance Needed - Invalid Argument Error in Roblox Script

Hello Roblox Developer Community,

I hope this message finds you well. I am currently facing an issue with a script in my Roblox game and would greatly appreciate your assistance.

Error Description: I have a script that is intended to create a snowball object and throw it based on player input. However, I am encountering the following error:

14:58:17.225 Players.gabrielcardoso06.Backpack.Snowball.msn:18: invalid argument #2 to ‘new’ (Vector3 expected, got nil) - Server - msn:18

Script Snippet:

local snowBall = script.Parent
local directon

script.Parent.PickenTarget.OnServerEvent:Connect(function(player, mouse)
    directon = mouse
end)

local function throw()
    local char = snowBall.Parent
    local hp = char:WaitForChild("RightHand")
    print(hp)
    local player = snowBall.Parent
    
    local Handle = snowBall.Handle

    local newSnowBall = Handle:Clone()
    newSnowBall.Parent = workspace
    newSnowBall.CanTouch = false
    newSnowBall.CanCollide = true
    newSnowBall.CFrame = CFrame.new(newSnowBall.Position + newSnowBall.CFrame.LookVector * 3, directon)
    newSnowBall.AssemblyLinearVelocity = Vector3.new(100, 100, 100)
end

snowBall.Activated:Connect(throw)

Issue Analysis: The error seems to occur in the throw function at line 18, where the CFrame.new function is being called. It suggests that there is an invalid argument (#2) being passed, with a Vector3 expected, but nil is being provided instead.

Request for Assistance: I kindly ask for your expertise in helping me identify and resolve the issue. If you have any insights into why this error might be occurring or suggestions for improvement in the script, I would be extremely grateful.

Thank you in advance for your time and assistance. I look forward to learning from the experienced developers in our community.

Your variable directon is likely nil.

Are you sure that script.Parent.PickenTarget.OnServerEvent is firing BEFORE snowBall.Activated?

Either the tool is being activated before you’ve fired PickenTarget or you’re not firing it at all. (Or you’re not actually passing the mouse variable as a Vector with the event)

How can I solve that? I dont know how to solve this

Print directon inside both of your functions to see when it is defined/what is it.

Do you ever use :FireServer(mouse) on your “PickenTarget” RemoteEvent?

what’s that direction variable for?

To throw the snowball in mouse direction

I did this:

local snowBall = script.Parent
local directon

script.Parent.PickenTarget.OnServerEvent:Connect(function(player, mouse)
	directon = mouse
	print(directon)
end)

local function throw()
	local Handle = snowBall.Handle
	local newSnowBall = Handle:Clone()
	newSnowBall.Parent = workspace
	newSnowBall.CanTouch = false
	newSnowBall.CanCollide = true
	newSnowBall.CFrame = CFrame.new(newSnowBall.Position + newSnowBall.CFrame.LookVector * 3, directon)
	newSnowBall.AssemblyLinearVelocity = Vector3.new(100, 100, 100)
	print(directon)
end

snowBall.Activated:Connect(throw)

and output said:

image

local script:

local player =game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	script.Parent.PickenTarget:FireServer(mouse.Hit.p)
end)

The last output in console is when directon is defined.
image
If you look the error occurs before we know the variable is defined.

image

If you’re already passing the data through a remoteEvent on activation, then why listen for activation on the server? Why not put all of your code inside the .OnServerEvent? (I’m assuming that you’re listening for .Activated on the same tool from both the client and server.)

1 Like

I did what you said, and the error stopped, but the snowball is not being thrown at the mouse position. How can I solve this?

I am not experienced with the math for this but I can tell you that you appear to be just setting the position and rotation of the part and giving it AssemblyLinearVelocity not towards any particular direction.

This post would likely answer your question better than I can

1 Like