Help with Mouse.Position and Raycasting

I am trying to make a simple hit scan gun but I have this error

attempt to perform arithmetic (mul) on Vector3 and Instance

I dont know why this is happening as both Camera.Position and Hit.Position are both Vector3

here are the client and server scripts

Client

local GunSystem = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local ShotEvent = GunSystem:WaitForChild("FireGun")

GunSystem.Activated:Connect(function()
	local Mouse = player:GetMouse()
	local Camera = game.Workspace.CurrentCamera
	
	local TargetPosition = Mouse.Hit.Position
	local StartPosition = Camera.CFrame.Position
	print(TargetPosition,StartPosition)
	ShotEvent:FireServer(StartPosition, TargetPosition)
end)

Server

script.Parent.FireGun.OnServerEvent:Connect(function(player, StartPosition, TargetPosition)
	local character = player.Character
	if not character then return end

	local rayDirection = (TargetPosition - StartPosition).unit * script.Parent.MaxDistance -- Error "attempt to perform arithmetic (mul) on Vector3 and Instance"

	-- raycast camera to target
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character} -- Ignore character
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local raycastResult = workspace:Raycast(StartPosition, rayDirection, raycastParams)

	if raycastResult then
		print(raycastResult.Instance.Name)
		
		local humanoid =  raycastResult.Instance.Parent:FindFirstChild("Humanoid")
		
		if humanoid then
			humanoid:TakeDamage(script.Parent.Damage) -- Damage hit humanoid
		end
	end
end)

I’m not completely sure what this error means so if anyone can help It would be greatly appreciated

You are trying to multiply an instance with a vector3, you probably forgot to index the property.

(ty for the correction rotiv)

2 Likes

If I remember right, you can multiply numbers and vectors, and the error is saying that it is trying to multiply a vector with an instance (Object), so whatever you are getting that maximum distance is an instance. You’re probably getting it by a number value so you should try "“script.Parent.MaxDistance.Value” instead

1 Like

Max distance is an object (value maybe?) and not a number. If it’s a value then do …MaxDistance.Value. If it isn’t then make sure it’s a int/number value

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.