So I recently picked up raycasting and started to experiment with it. Everything works fine without two things, number one I can't see the ray part. Number two, after I did my best to implement a spread system the gun broke down, it doesn't hit anything. So I added a print(hit) which always returns nil when I got the spread system.
Edit: forgot to add the spread part before, it's fixed now though
--Gun local tool = script.Parent local player = game:GetService("Players").LocalPlayer local character = player.Character or player.CharacterAdded:wait() local human = character:WaitForChild("Humanoid") local handle = tool.Handle local mouse = player:GetMouse() local clip = 30 local ammo = 90 local equipped = nil local reloading = false local damage = 10 local IsFiring = nil local spreadX = 0 local spreadY = 0 local spreadZ = 0 local spreadXMax = 1.1 local spreadYMax = 0.3 local spreadZMax = 1.1 local damageModifier = { 1.5, 1, 0.7, 0.5 } print(tonumber(damageModifier[1])) local lastfire = 0 local rps = 0 local shootwindow = 0.5 tool.Equipped:connect(function() -- shooting equipped = true mouse.Button1Down:connect(function() if clip >= 1 and reloading == false then print("firedonce") repeat print("fires") local clickdelta = tick() -lastfire -- spread if clickdelta > 1 then lastfire = tick() if clickdelta < shootwindow then rps = rps + 1 if rps >= 4 then spreadX = spreadX + 0.0423 spreadZ = spreadZ + 0.0423 spreadY = spreadY + 0.0115 if spreadX > spreadXMax then spreadX = spreadXMax return end if spreadY > spreadYMax then spreadY = spreadYMax return end if spreadZ > spreadZMax then spreadZ = spreadZMax return end end end end local ray = Ray.new( handle.CFrame.p, --origin (mouse.hit.p - handle.CFrame.p).unit + Vector3.new(math.random(0,spreadX),math.random(0,spreadY),math.random(0,spreadZ))* 500 --direction ) local hit, position = workspace:FindPartOnRay(ray, character,nil) local bullet = Instance.new("Part", workspace) --Part bullet.BrickColor = BrickColor.new("Yellow") bullet.FormFactor = "Custom" bullet.Material = "Neon" bullet.Transparency = 0.25 bullet.Locked = true bullet.Anchored = true bullet.CanCollide = false local distance = (handle.CFrame.p - position).magnitude bullet.Size = Vector3.new(0.3,0.3,distance) bullet.CFrame = CFrame.new(handle.CFrame.p,position) * CFrame.new(0,0, -distance / 2) game:GetService("Debris"):AddItem(bullet,1) clip = clip - 1 print(clip) IsFiring = true print(hit.Name) if hit and hit ~= nil then print(hit.Name) if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == "Zombie" then --Damage local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then if hit.Name == "Head" then humanoid:TakeDamage(damage * tonumber(damageModifier[1])) elseif hit.Name == "Torso" then humanoid:TakeDamage(damage * tonumber(damageModifier[2])) elseif hit.Name == "Right Arm" or hit.Name == "Left Arm" then humanoid:TakeDamage(damage * tonumber(damageModifier[3])) elseif hit.Name == "Right Leg" or hit.Name == "Left Leg" then humanoid:TakeDamage(damage * tonumber(damageModifier[4 ])) end end end end wait(0.115) until clip <= 0 or IsFiring == false end end) mouse.Button1Up:connect(function() --stop shooting IsFiring = false end) mouse.KeyDown:connect(function(key) --reload key:lower() if key == "r" and reloading == false and equipped == true and clip <= 29 and ammo >= 1 then for i = 1,30 do wait() if clip <= 29 and ammo >= 1 then clip = clip + 1 ammo = ammo - 1 print(clip) print(ammo) else break end end end end) end) tool.Unequipped:connect(function() equipped = false end)
Thanks in advance, Kryddan!