Why does my gun fire all guns in the server when fired?
Hi! I made a gun script, local script, and remote event to communicate between the local and server script. When two or more players have there guns out at the same time and I click to fire the gun all the players with their guns out will fire their guns. Here is the local script:
01 | local player = game.Players.LocalPlayer |
02 | local character = player.CharacterAdded:Wait() |
03 | local mouse = player:GetMouse() |
04 | local tool = script.Parent |
06 | tool.Equipped:Connect( function () |
07 | tool.Activated:Connect( function () |
08 | local mouse_location = mouse.Hit.Position |
09 | local bullet_ray = Ray.new(tool.Main.CFrame.Position,(mouse_location - tool.Main.CFrame.Position).unit * 300 ) |
10 | local part,position = workspace:FindPartOnRay(bullet_ray,character, false , false ) |
11 | game.ReplicatedStorage.Lee.fired:FireServer(character,mouse_location,position,part) |
Here is the server script:
01 | local tool = script.Parent |
03 | UIS = game:GetService( "UserInputService" ) |
05 | game.ReplicatedStorage.Lee.fired.OnServerEvent:Connect( function (player,character,mouse_location,position,part) |
06 | if tool.Ammo.Value > 0 then |
09 | tool.Ammo.Value = tool.Ammo.Value - 1 |
11 | local bullet = Instance.new( "Part" ,game.Workspace) |
12 | bullet.BrickColor = BrickColor.new( "New Yeller" ) |
13 | bullet.Transparency = 0 |
14 | bullet.Material = Enum.Material.SmoothPlastic |
15 | bullet.Anchored = true |
17 | bullet.CanCollide = false |
19 | local distance = (tool.Main.CFrame.Position - position).Magnitude |
20 | bullet.Size = Vector 3. new( 0.2 , 0.2 ,distance) |
21 | bullet.CFrame = CFrame.new(tool.Main.CFrame.Position,position) * CFrame.new( 0 , 0 ,-distance/ 2 ) |
23 | game:GetService( "Debris" ):AddItem(bullet, 0.1 ) |
24 | tool.GunSounds.GunFired:Play() |
27 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
28 | if humanoid ~ = nil then |
29 | humanoid:TakeDamage( 50 ) |
30 | if humanoid.Health < = 0 then |
31 | player.PlayerGui.killGui.killFrame.killLabel.Text = "You killed " .. tostring (humanoid.Parent.Name) .. "!" |
33 | player.PlayerGui.killGui.killFrame.killLabel.Text = "" |
37 | tool.Main.smoke.Enabled = true |
38 | tool.Main.fire.Enabled = true |
39 | tool.Main.light.Enabled = true |
41 | tool.Main.smoke.Enabled = false |
42 | tool.Main.fire.Enabled = false |
43 | tool.Main.light.Enabled = false |
44 | tool.GunSounds.GunChamber:Play() |
45 | local animation = character:WaitForChild( "Humanoid" ):LoadAnimation(tool.prime) |
51 | tool.GunSounds.OutOfAmmoFired:Play() |
Thanks!