Problems with gun firing mechanism?

So I am trying to make a gun system, and I am working on the firing part now. The problem is that the gun is having problems when I try to create the ray. My script looks like:

function LaserFunctions.DrawLaser(plr, mouse)
local accuracy = Settings.Accuracy
local maxfiredistance = Settings.MaxDistance
local Ignore = plr.Character
local ray = Ray.new(tool.FirePart.CFrame.p,(mouse.Position - tool.FirePart.CFrame.p +  Vector3.new(math.random(-accuracy,accuracy),math.random(-accuracy,accuracy),math.random(-accuracy,accuracy))).unit * maxfiredistance)
local part, position = workspace:FindPartOnRay(ray, Ignore, false, true)
local human
if part then
	if part.Parent then
		local human = part.Parent:FindFirstChild("Humanoid")
		if not human then
			human = part.Parent.Parent:FindFirstChild("Humanoid")
		end
		if human then
			human = human
		end
	end
end
return ray, human, position
end

The code that calls that:

function GunFunctions.Fire(plr, mouse)
	if currentammo > 0 then
		local ray, touchedhuman, position = LaserFunctions:DrawLaser(plr, mouse)
	else
		if Settings.AutoReload == true then
			GunFunctions.Reload(plr)
		end
	end
end

GunFunctions.Fire is called by a serversided script with the arguments of plr, mouse.Hit. Whenever I do this script, the output errors and says that “Position is not a member of Player” When I tried to switch the player and mouse variables around, it said that “Expected Vector3, got nil.” When I printed mouse.Position and plr in the GunFunctions.Fire, it printed the mouse position and the plr name. When I tried printing it in the DrawLaser function, it printed table for plr and the plr name for mouse. This is very confusing, and any help is appreciated.

does this give an error if yes can I see it?

I said that in my post.
30 characters

I fired the mouse.Hit from the client in the beginning

im going to try testing your code to see

Make sure to fill in the Settings places so the script doesn’t error.

your first part seems to be fine so lets cross that out if you think not this is what i did

local LaserFunctions = {}
function LaserFunctions.DrawLaser(plr, mouse)
local accuracy = 100
local maxfiredistance = 100
local Ignore = nil
local ray = Ray.new(Vector3.new(0,0,0),(Vector3.new(0,0,0) - Vector3.new(0,0,0) +  Vector3.new(math.random(-accuracy,accuracy),math.random(-accuracy,accuracy),math.random(-accuracy,accuracy))).unit * maxfiredistance)
local part, position = workspace:FindPartOnRay(ray, Ignore, false, true)
local human
if part then
	if part.Parent then
		local human = part.Parent:FindFirstChild("Humanoid")
		if not human then
			human = part.Parent.Parent:FindFirstChild("Humanoid")
		end
		if human then
			human = human
		end
	end
end
return ray, human, position
end
print(LaserFunctions.DrawLaser()) --prints the returns
1 Like

@CaptainD_veloper
You’re calling a function with : when it was defined with a ., causing it to run the code with different behavior. plr is treated as the table of that function that was called in (commonly known as self), mouse is treated as plr. Use . when calling functions as defined in the code itself.

local ray, touchedhuman, position = LaserFunctions.DrawLaser(plr, mouse)
2 Likes

Thanks, that fixed it! Now my code runs much better.