Raycast Part Not Working

I am making a gun system, but I get an error

local BulletLifeTime = .05; 
local FireRate = .05; 
local ReloadTime = 1;
local MagSize = 31; 
local Damage = 10;
local HeadshotAddition = 10;
local Automatic = true;
local Range = 1500; 
local AutoReload = true; 
local VisibleBulletRaycast = true;
local BulletRaycastThickness = .1
local Spread = 1
local ProduceLight = true




db = false
local rld = false
local ammo = MagSize
local plr = game.Players.LocalPlayer;
repeat wait() until plr.Character;
local char = plr.Character;
local mouse = plr:GetMouse()
local tool, FirePos = script.Parent, script.Parent:FindFirstChild("FirePos");
local r = game:GetService("RunService")
local hold = false
local toolname = script.Parent.Name
local enabled = false

tool.ToolTip = "Damage "..Damage.." | Headshot +"..HeadshotAddition.." | Magsize "..MagSize.." | FireRate "..FireRate.." | Automatic = "..tostring(Automatic).." | Spread "..Spread.." | ReloadTime "..ReloadTime

local function reload()
	if ammo <= 31 then
		if rld == false then 
			rld = true
			if db == false then
				db = true
				mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
				script.ReloadEvent:FireServer()
				tool.Reload:Play()
				tool.Name = "."
				wait(ReloadTime/3)
				tool.Name = ".."
				wait(ReloadTime/3)
				tool.Name = "..."
				wait(ReloadTime/3)
				ammo = MagSize
				tool.Name = toolname
				mouse.Icon = "rbxasset://textures\\GunCursor.png"
			end
		end
		rld = false
		db = false
	end
end


local function FireRay()
	if db == false and rld == false then
		db = true

		if ammo > 0 then
			tool.Shot:Play()
			ammo = ammo - 1
			script.FireEvent:FireServer()
			local raycaststuffidk = RaycastParams.new()
			raycaststuffidk.IgnoreWater = false
			raycaststuffidk.FilterType = Enum.RaycastFilterType.Exclude
			local ray = workspace:Raycast(FirePos.Position,(mouse.Hit.p-FirePos.Position--[[ + Vector3.new(math.random(-Spread,Spread),math.random(-Spread,Spread),math.random(-Spread,Spread))]]).unit*Range,raycaststuffidk)
			local hit,pos = nil,nil
			if ray then
				hit = ray.Instance
				pos = ray.Position
				if hit.Parent:FindFirstChild("Humanoid") then
					script.Parent.DamagePlayerEvent:FireServer(hit.Parent:FindFirstChild("Humanoid"),Damage)
					if hit.Name == "Head" then
						script.Parent.HeadShotEvent:FireServer(hit.Parent:FindFirstChild("Humanoid"),HeadshotAddition)
					end
				end
			end

			if VisibleBulletRaycast == true then
				local p = Instance.new("Part",workspace)
				p.Anchored = true
				p.CanCollide = false
				p.Name = "Bullet"
				p.BrickColor = BrickColor.new("New Yeller")
				p.FormFactor = "Custom"
				p.TopSurface = "Smooth"
				p.BottomSurface = "Smooth"
				p.CastShadow = false
				p.Size = Vector3.new(BulletRaycastThickness,BulletRaycastThickness,pos)
				p.CFrame = CFrame.new(pos,FirePos.Position)*CFrame.new(0,0,-pos)
				game:GetService("Debris"):AddItem(p,BulletLifeTime)
			end
			if ProduceLight == true then
				--script.Parent.FirePos.Fire.Enabled = true
				script.Parent.FirePos.PointLight.Enabled = true
				wait(0.001)				
				--script.Parent.FirePos.Fire.Enabled = false
				script.Parent.FirePos.PointLight.Enabled = false
			end

		end

		wait(FireRate)

		db = false
	end
end

on this line, i don’t know what to put for pos

	local ray = workspace:Raycast(FirePos.Position,(mouse.Hit.p-FirePos.Position--[[ + Vector3.new(math.random(-Spread,Spread),math.random(-Spread,Spread),math.random(-Spread,Spread))]]).unit*Range,raycaststuffidk)
			local hit,pos = nil,ray.Position

it errors here

				p.CFrame = CFrame.new(pos,FirePos.Position)*CFrame.new(0,0,-pos)

error:

Players.Miserable_Haven.Backpack.AK-47.MainScript:71: attempt to index nil with 'Position' 

also, when i shoot anywhere, the raycast yellow part does not appear.

This means that the script tries to index FirePos.Position, but FirePos is nil, so it is unable to index the Position property.

Your FirePos seems to have been set to nil at some point of your script. Can we know more about FirePos?

the selected part is firepos
image

But thats not the problem i think

It is the problem. Your FirePart somehow has became nil. Can you try to check if that part still exists when you run the script?

oh, sorry i got this fixed yesterday i forgot about the post though

I also forgot how i fixed it

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