Script:
Ammo = 45 damage = 8 AbleToFire = true isFiring = true Player = game.Players.LocalPlayer mouse = Player:GetMouse() script.Parent.Equipped:connect(function(eventMouse) Player:WaitForChild("PlayerGui").ScreenGui3.Frame.Visible = true eventMouse.Icon = "rbxasset://textures\\GunCursor.png" local Arm = Player.Character["Right Arm"] local Torso = Arm.Parent.Torso local Weld = Player.Character.Torso:WaitForChild("Weld3") Weld.Part0 = Torso Weld.Part1 = Arm Weld.C1 = CFrame.Angles(math.rad(-90), -1, math.rad(0)) * CFrame.new(-1, -0.8, 0.8) end) script.Parent.Unequipped:connect(function() Player:WaitForChild("PlayerGui").ScreenGui3.Frame.Visible = false local Weld = Player.Character.Torso:WaitForChild("Weld3") Weld.Part0 = nil Weld.Part1 = nil end) script.Parent.Activated:connect(function() isFiring = true repeat wait(.0600) if Ammo > 0 and AbleToFire == true then script.Parent.FireSound:Play() local ray = Ray.new(script.Parent.Handle.CFrame.p, (mouse.Hit.p - script.Parent.Handle.CFrame.p).unit * 300) local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true) Bullet = Instance.new("Part", workspace) game:GetService("Debris"):AddItem("Bullet", 0.1) Bullet.CanCollide = true Bullet.BrickColor = BrickColor.new("New Yeller") Bullet.FormFactor = "Custom" Bullet.Material = "Neon" Bullet.CanCollide = true Bullet.Name = "M4A1Bullet" local distance = (script.Parent.Handle.CFrame.p - position).magnitude Bullet.Size = Vector3.new(.2, .2, distance) Bullet.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2) if part then local humanoid = part.Parent:FindFirstChild("Humanoid") if not humanoid then humanoid = part.Parent.Parent:FindFirstChild("Humanoid") end if humanoid then player = game.Players:GetPlayerFromCharacter(part.Parent) if player.TeamColor ~= Player.TeamColor then humanoid:TakeDamage(damage) end end end script.Parent.GripPos = Vector3.new(0.15, -0.4, 0.95) wait(0) script.Parent.GripPos = Vector3.new(0.15, -0.4, 1) Ammo = Ammo - 1 Player.PlayerGui.ScreenGui3.Ammo.Value = Player.PlayerGui.ScreenGui3.Ammo.Value - 1 end until isFiring == false or Ammo == 0 end) script.Parent.Deactivated:connect(function() isFiring = false end) mouse.KeyDown:connect(function(Key) if Key == "r" then if Ammo <= 44 then if AbleToFire then AbleToFire = false AbleToFire = false script.Parent.ReloadSound:Play() wait(1.986) Ammo = 45 Player.PlayerGui.ScreenGui3.Ammo.Value = 45 AbleToFire = true end end end end)
That's the script, can someone tell me what I've done wrong or how to fix it? Thanks in all regards.
The problem here isn't the raycasting itself; it's because of the fact that your mouse is clicking on the player's accessories. To fix this, use TargetFilter: http://wiki.roblox.com/index.php?title=API:Class/Mouse/TargetFilter
Code:
mouse = Player:GetMouse() mouse.TargetFilter = Player.Character
Either this doesn't work, or I put the mouse.TargetFilter = Player.Character
in the wrong row.
Script:
Ammo = 45 damage = 8 AbleToFire = true isFiring = true Player = game.Players.LocalPlayer mouse = Player:GetMouse() script.Parent.Equipped:connect(function(eventMouse) Player:WaitForChild("PlayerGui").ScreenGui3.Frame.Visible = true eventMouse.Icon = "rbxasset://textures\\GunCursor.png" local Arm = Player.Character["Right Arm"] local Torso = Arm.Parent.Torso local Weld = Player.Character.Torso:WaitForChild("Weld3") Weld.Part0 = Torso Weld.Part1 = Arm Weld.C1 = CFrame.Angles(math.rad(-90), -1, math.rad(0)) * CFrame.new(-1, -0.8, 0.8) local Weld2 = Player.Character.Torso:WaitForChild("Weld4") local Arm2 = Player.Character["Left Arm"] Weld.Part0 = Torso Weld.Part1 = Arm2 Weld.C1 = CFrame.Angles(math.rad(-90), 1, math.rad(0)) * CFrame.new(-0.3, -0.5, 0.8) end) script.Parent.Unequipped:connect(function() Player:WaitForChild("PlayerGui").ScreenGui3.Frame.Visible = false local Weld = Player.Character.Torso:WaitForChild("Weld3") Weld.Part0 = nil Weld.Part1 = nil end) script.Parent.Activated:connect(function() isFiring = true repeat wait(.0600) if Ammo > 0 and AbleToFire == true then script.Parent.FireSound:Play() local ray = Ray.new(script.Parent.Handle.CFrame.p, (mouse.Hit.p - script.Parent.Handle.CFrame.p).unit * 300) local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true) Bullet = Instance.new("Part", workspace) Bullet.CanCollide = true Bullet.BrickColor = BrickColor.new("New Yeller") Bullet.FormFactor = "Custom" Bullet.Material = "Neon" Bullet.CanCollide = false Bullet.Anchored = true Bullet.Locked = true Bullet.Name = "M4A1Bullet" mouse.TargetFilter = Player.Character local distance = (script.Parent.Handle.CFrame.p - position).magnitude Bullet.Size = Vector3.new(.0500, .0500, distance) Bullet.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2) game:GetService("Debris"):AddItem(Bullet, 0.1) if part then local humanoid = part.Parent:FindFirstChild("Humanoid") if not humanoid then humanoid = part.Parent.Parent:FindFirstChild("Humanoid") end if humanoid then player = game.Players:GetPlayerFromCharacter(part.Parent) if player.TeamColor ~= Player.TeamColor then humanoid:TakeDamage(damage) end end end script.Parent.GripPos = Vector3.new(0.15, -0.4, 0.95) wait(0) script.Parent.GripPos = Vector3.new(0.15, -0.4, 1) Ammo = Ammo - 1 Player.PlayerGui.ScreenGui3.Ammo.Value = Player.PlayerGui.ScreenGui3.Ammo.Value - 1 end until isFiring == false or Ammo == 0 end) script.Parent.Deactivated:connect(function() isFiring = false end) mouse.KeyDown:connect(function(Key) if Key == "r" then if Ammo <= 44 then if AbleToFire then AbleToFire = false AbleToFire = false script.Parent.ReloadSound:Play() wait(1.986) Ammo = 45 Player.PlayerGui.ScreenGui3.Ammo.Value = 45 AbleToFire = true end end end end)