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

Ray guns firing from all people?

Asked by 5 years ago

I made a simple raygun for my game, though when it fires, it fires from all humanoids. I have no idea why this is happening, any help?

gun link: https://www.roblox.com/catalog/02597468354/redirect

1
script DinozCreates 1070 — 5y
0
plz User#23365 30 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Here's the problem. The server script is being replicated across all clients and responds to that remote event. So, when one client fires the server, all the clients receive that event. Therefore, all clients are creating the laser from their gun. And here's a solution:

You could add the player as a parameter, and only fire the laser from their gun. It would work something like this.

Local Script

script.Parent.Activated:Connect(function()
    local mouse = game.Players.LocalPlayer:GetMouse()
    game.ReplicatedStorage.BulletHandler:FireServer(mouse.Hit.p, game.Players.LocalPlayer)
end)

Server Script

debounce = false

game.ReplicatedStorage.BulletHandler.OnServerEvent:Connect(function(plr, mousePos)
    if plr == game.Players:GetPlayerFromCharacter(script.Parent.Parent) then

        if debounce == false then

        debounce = true

        local beam = Instance.new("Part",game.Workspace)
        beam.Material = "Neon"
        --color Changes based on name
        if script.Parent.Name == "RedGun" then
            beam.BrickColor = BrickColor.Red()
        elseif script.Parent.Name == "BluGun" then
            beam.BrickColor = BrickColor.Blue()
        end
        -- will change to have color based on team
        beam.Anchored = true
        beam.CanCollide = false

        local ray = Ray.new(script.Parent.Part.Position , (mousePos - script.Parent.Handle.Position).unit * 300)
        local obj, endpoint = workspace:FindPartOnRay(ray, script.Parent.Parent)

        local distance = (script.Parent.Handle.Position - endpoint).magnitude
        beam.Size = Vector3.new(1,1, distance)
        beam.CFrame = CFrame.new(script.Parent.Handle.Position, endpoint) * CFrame.new(0,0, -distance / 2)

        if obj then 
            local humanoid = obj.Parent:FindFirstChild("Humanoid")
            if humanoid then
                humanoid:TakeDamage(20)
            end
        end

        game:GetService("Debris"):AddItem(beam, .2)

        wait(.5)
        debounce = false

        end
    end
end)
Ad

Answer this question