Why does this error appear?

so i made this script but this is where it errors on this line:

local Direction = (TargetPosition - OriginPosition).Unit * range

the error message is:

layers.ForgottenDogYT.Backpack.MegaGun2.Client:36: attempt to perform arithmetic (sub) on nil

You most likely haven’t set the TargetPosition to any value.

I set it. Thats just the segment of the script. this is the whole script:

local tool = script.Parent
local FirePoint = tool.Handle:FindFirstChild("GunFirePoint")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local BulletFolder = game.Workspace.Bullets
local BulletFireEvent = game.ReplicatedStorage.GunRemotes.MegaGun.MegaGunFire
local range = 200
local debris = game:GetService("Debris")

mouse.TargetFilter = BulletFolder

tool.Activated:Connect(function(player, TargetPosition, OriginPosition)
	local MousePosition = mouse.Hit.p
	local FirePosition = FirePoint.CFrame.Position
	
	BulletFireEvent:FireServer(MousePosition, FirePosition)
	
	local function CreateBeam(OriginPosition, TargetPosition)
	local Distance = (TargetPosition - OriginPosition).Magnitude

	local part = Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Really red") -- you can also use RGB for more options ex.
	-- part.Color = Color3.fromRGB(255, 0, 0)
	
	part.Size = Vector3.new(.5, .5, Distance)
	part.CFrame =  CFrame.lookAt(OriginPosition, TargetPosition) * CFrame.new(0, 0, -Distance/2)
	
	part.Parent = BulletFolder
	
	debris:AddItem(part, 1)
end
	
	local Direction = (TargetPosition - OriginPosition).Unit * range
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = {player.Character,script.Parent.Handle,script.Parent.Handle.GunFirePoint}
	local Result = workspace:Raycast(OriginPosition, Direction, RayParams)
	
	if Result then
		print(Result.Instance)
		local velocity = Instance.new("BodyVelocity")
		local Character = Result.Instance.Parent
		local humanoid = Character:FindFirstChild("Humanoid")
		
		CreateBeam(OriginPosition, Result.Position)
		if humanoid and humanoid ~= player.Character.Humanoid then
			humanoid:ChangeState(Enum.HumanoidStateType.PlatformStanding)
			wait(0.05)
			Character.HumanoidRootPart:ApplyImpulse(-Direction.Unit * 300)
			wait(0.2)
			humanoid:TakeDamage(100)
		end
		
	else
		CreateBeam(OriginPosition, OriginPosition + Direction)
	end
	
end)

Looking at the devhub page on Tool.Activated, i don’t see any arguments given by the event…?

Oh I understand. The variable “TargetPosition” ceases use when you add the end in line 34. What I would instead do is put

local Direction

in line 9, and put line 36 just before the end in line 34. This should solve your problem, and if it doesn’t be sure to get back to me.

To be more clear It’d look like this

local tool = script.Parent
local FirePoint = tool.Handle:FindFirstChild("GunFirePoint")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local BulletFolder = game.Workspace.Bullets
local BulletFireEvent = game.ReplicatedStorage.GunRemotes.MegaGun.MegaGunFire
local range = 200
local debris = game:GetService("Debris")
local Direction

mouse.TargetFilter = BulletFolder

tool.Activated:Connect(function(player, TargetPosition, OriginPosition)
	local MousePosition = mouse.Hit.p
	local FirePosition = FirePoint.CFrame.Position
	
	BulletFireEvent:FireServer(MousePosition, FirePosition)
	
	local function CreateBeam(OriginPosition, TargetPosition)
	local Distance = (TargetPosition - OriginPosition).Magnitude

	local part = Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Really red") -- you can also use RGB for more options ex.
	-- part.Color = Color3.fromRGB(255, 0, 0)
	
	part.Size = Vector3.new(.5, .5, Distance)
	part.CFrame =  CFrame.lookAt(OriginPosition, TargetPosition) * CFrame.new(0, 0, -Distance/2)
	
	part.Parent = BulletFolder
	
	debris:AddItem(part, 1)

	local Direction = (TargetPosition - OriginPosition).Unit * range
end
	
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = {player.Character,script.Parent.Handle,script.Parent.Handle.GunFirePoint}
	local Result = workspace:Raycast(OriginPosition, Direction, RayParams)
	
	if Result then
		print(Result.Instance)
		local velocity = Instance.new("BodyVelocity")
		local Character = Result.Instance.Parent
		local humanoid = Character:FindFirstChild("Humanoid")
		
		CreateBeam(OriginPosition, Result.Position)
		if humanoid and humanoid ~= player.Character.Humanoid then
			humanoid:ChangeState(Enum.HumanoidStateType.PlatformStanding)
			wait(0.05)
			Character.HumanoidRootPart:ApplyImpulse(-Direction.Unit * 300)
			wait(0.2)
			humanoid:TakeDamage(100)
		end
		
	else
		CreateBeam(OriginPosition, OriginPosition + Direction)
	end
	
end)

Thats not the issue,

script.Parent.Activated:Connect(function(...)
    print(...)
end)

I put that code in a local script inside a tool, there is no arguments given meaning TargetPosition and OriginPosition are nil

image

The OP should use Raycasting or something like that

what can I do then? Since it cant give arguments