Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my gun fire all guns in the server when fired?

Asked by 4 years ago

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:

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local tool = script.Parent

tool.Equipped:Connect(function()
    tool.Activated:Connect(function()
        local mouse_location = mouse.Hit.Position
        local bullet_ray = Ray.new(tool.Main.CFrame.Position,(mouse_location - tool.Main.CFrame.Position).unit * 300)
        local part,position = workspace:FindPartOnRay(bullet_ray,character,false,false)
        game.ReplicatedStorage.Lee.fired:FireServer(character,mouse_location,position,part)
    end)
end)

Here is the server script:

local tool = script.Parent
local enabled = false
UIS = game:GetService("UserInputService")

game.ReplicatedStorage.Lee.fired.OnServerEvent:Connect(function(player,character,mouse_location,position,part)
    if tool.Ammo.Value > 0 then
        if not enabled then
            enabled = true
            tool.Ammo.Value = tool.Ammo.Value - 1

            local bullet = Instance.new("Part",game.Workspace)
            bullet.BrickColor = BrickColor.new("New Yeller")
            bullet.Transparency = 0
            bullet.Material = Enum.Material.SmoothPlastic
            bullet.Anchored = true
            bullet.Locked = true
            bullet.CanCollide = false

            local distance = (tool.Main.CFrame.Position - position).Magnitude
            bullet.Size = Vector3.new(0.2,0.2,distance)
            bullet.CFrame = CFrame.new(tool.Main.CFrame.Position,position) * CFrame.new(0,0,-distance/2)

            game:GetService("Debris"):AddItem(bullet,0.1)
            tool.GunSounds.GunFired:Play()

            if part ~= nil then
                local humanoid = part.Parent:FindFirstChild("Humanoid")
                if humanoid ~= nil then
                    humanoid:TakeDamage(50)
                    if humanoid.Health <= 0 then
                        player.PlayerGui.killGui.killFrame.killLabel.Text = "You killed " .. tostring(humanoid.Parent.Name) .. "!"
                        wait(3)
                        player.PlayerGui.killGui.killFrame.killLabel.Text = ""
                    end
                end
            end
            tool.Main.smoke.Enabled = true
            tool.Main.fire.Enabled = true
            tool.Main.light.Enabled = true
            wait(0.2)
            tool.Main.smoke.Enabled = false
            tool.Main.fire.Enabled = false
            tool.Main.light.Enabled = false
            tool.GunSounds.GunChamber:Play()
            local animation = character:WaitForChild("Humanoid"):LoadAnimation(tool.prime)
            animation:Play()
            wait(2)
            enabled = false
        end
    else
        tool.GunSounds.OutOfAmmoFired:Play()
    end
end)

Thanks!

0
maybe make a variable of game.Players.LocalPlayer Idrk tbh kingblaze_1000 359 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Problem fixed! All I had to do was delete:

local tool = script.Parent

in the server script then add the variable "tool" in the local script to the remote event!

Ad

Answer this question