Hello, I need help with my gun script

Hello So my gun works, but the trouble is that when I shot to the sky, the part doesn’t shows, like, part just doesn’t show, is not even added to Ignore_Model model, any idea why? Code:

local tool = script.Parent
repeat wait() until tool.Parent:FindFirstChildWhichIsA("Humanoid")
local Ammo = script.Parent.Ammo.Value
local StoredAmmo = script.Parent.StoredAmmo.Value
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local mouse = Player:GetMouse()
local Animations = script.Parent.Animations
local Equipped = false
local events = script.Parent.Events
local Character = Player.Character
local Spread = 12
local Camera = game.Workspace.CurrentCamera
local Ignore_Model = game.Workspace:FindFirstChild("Ignore_Model") or Instance.new("Model",game.Workspace)
Ignore_Model.Name = "Ignore_Model"
local Ray_Ignore = {Ignore_Model, Camera}

function equip()
	local function retry(Animation)
		Player.Character.Humanoid:LoadAnimation(Animations.HoldAnimation):Play()
	end
	local success, fail = pcall(function()
		Player.Character.Humanoid:LoadAnimation(Animations.HoldAnimation):Play()
		Equipped = true
	end)
	if success then
		print("Successfull loaded animation")
	elseif fail then
		retry(Animations.HoldAnimation)
		Equipped = true
	end
end

function unequip()
	for i,v in pairs(Player.Character.Humanoid:GetPlayingAnimationTracks()) do
		v:Stop()
		
	end
	Equipped = false
end

function Shot()
local Spread = CFrame.Angles(math.rad(math.random(-Spread, Spread)/10), math.rad(math.random(-Spread, Spread)/10), math.rad(math.random(-Spread, Spread)/10))
			local Ray = Ray.new(Character.Head.Position, (CFrame.new(Character.Head.Position, mouse.Hit.Position ) * Spread).lookVector.unit * 6000)
			local Hit,Pos = game.Workspace:FindPartOnRay(Ray, Character)

   if Hit then
	print("Part Touched")
	events.Hit:FireServer(Hit)
	print(Hit.Parent.Name)
end

local distance = (tool.BulletSpawn.CFrame.Position - Pos).magnitude
   local Bullet = Instance.new("Part")
   Bullet.Transparency = 0
Bullet.Anchored = true
Bullet.BrickColor = BrickColor.new("Ghost grey")
Bullet.CFrame = CFrame.new(Pos, tool.BulletSpawn.CFrame.Position) * CFrame.new(0, 0, -distance/2)
Bullet.Size = Vector3.new(0, 0, distance)
Bullet.Parent = workspace
Bullet.CanCollide = false

game:GetService("Debris"):AddItem(Bullet, 0.1)

end


tool.Equipped:Connect(function()
	--equip()
	
	tool.Activated:Connect(function()
		Shot()
		print("click")
	end)
end)

Everything works, just the part doesnt shots when I aim to sky, or no hit parts.

1 Like

Is the shot fired in a mouse.button1down? and does it use Mouse.hit.p? or mouse.target?

1 Like

no, Tool.Activated function, and yes i’m using mouse.hit.position

1 Like

Thank you for doing that now please check that all the ends are lined up with an if and all end) are lined up with a function.
This will help anyone who reads through the script and will speed up their response.

Does the script error when you shoot towards the sky? If so paste the error here.

So, having spent some time working through your script and found a bit I am not sure about so here we go.
In the Shoot function you get a Hit and Pos variable set from FindPartOnRayWithIgnoreList.
You process the Hit in the if and then later use the Pos to get the distance.
So my concern is how do you expect the Pos to be set if there is no Hit?

I came to the same conclusion actually. I found it awkward when he wrote “local hit, pos…”. He is not really defining Pos.

So what should I do? :thinking:

Just make a seperate variable named Pos.
local Pos = mouse.Hit.p

I believe PartOnRay will supply values for both Hit and Pos but Pos is only valid if Hit is true.

1 Like

Still doesn’t work sirrrrrrr :thinking:

Are you sure no part appears in the Workspace Explorer? Have a look - the error could be that parts can only stretch to be 2048 studs (i think), so the beam wouldn’t be able to fit the whole distance of 6000 studs as you’ve written in the raycast Shot function.

2 Likes

Basically, it doesn’t spawns in the workspace, I’ve looked at explorer, and it doesn’t show at all :frowning: (like the part at explorer > workspace) so yeah, IDK what it can be

Can you try some basic debugging methods to narrow down where the error starts to occur?

1 Like