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

RemoteEvent firing on all clients?

Asked by 4 years ago

I scripted guns that use a RemoteEvent to get the player's mouse so the bullet can be aimed. However when playing the game when someone shot it made all other player's guns fire at the same spot. What is the error with my code? Local script:

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

local function onEquip()
    tool.GUI2.Parent = game.Players.LocalPlayer.PlayerGui
    mouse.Icon = "http://www.roblox.com/asset/?id=5110080608"
end

tool.Unequipped:Connect(function()
    game.Players.LocalPlayer.PlayerGui.GUI2.Parent = tool
    mouse.Icon = ""
end)

function onActivate()
    mouse = player:GetMouse()
    game.ReplicatedStorage.Shoot:FireServer(mouse.Hit)
end

tool.Activated:Connect(onActivate)
tool.Equipped:Connect(onEquip)

Server script:

local tool = script.Parent
local debounce = false
local remoteEvent = game.ReplicatedStorage.Shoot

local function onEquip()
    tool.Barrel.Equip:Play()
end


local function onActivate(player, mouse)
    if debounce == false then
        debounce = true
        if script.Parent.Ammo.Value ~= 0 then
        local Fire = Instance.new("Fire")
        Fire.Parent = tool.Muzzle
        Fire.Size = 2
        Fire.Color = Color3.new(255, 176, 0)
        Fire.SecondaryColor = Color3.new(255, 176, 0)
        local missile = game.Lighting.Pellet:Clone()
        missile.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame + script.Parent.Parent.HumanoidRootPart.CFrame.LookVector * 10
        missile.Parent = tool.Parent
        missile.BodyPosition.Position = mouse.Position
            tool.Muzzle.Fire:Play()
            tool.Ammo.Value = tool.Ammo.Value - 1
    wait(.3)
        Fire:Destroy()
    wait(.22)
            debounce = false
                    else
            tool.Barrel.Reload:Play()
            wait(1.5)
            tool.Ammo.Value = 7 
            wait(.22)
            debounce = false
end
    end
    end

tool.Equipped:Connect(onEquip)
tool.Activated:Connect(onActivate)
remoteEvent.OnServerEvent:Connect(onActivate)
0
Apologies, found the answer. I just needed to put the RemoteEvent in the guns themselves instead of ReplicatedStorage. Kirito40000000000 7 — 4y

Answer this question