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

How would I make my gun kill the player it hits?

Asked by 4 years ago
Edited 4 years ago
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer


tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    mouse.Button1Down:connect(function()
        print("Mouse pressed!")
        local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p - tool.Hole.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Bright yellow")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.1
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false


        local distance = (tool.Hole.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Hole.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)


        game:GetService("Debris"):AddItem(beam, 0.1)
    end)
end)

Here is my setup for the gun (picture): https://drive.google.com/file/d/1gJ1-DSUd-QKUfqNEh4LgBzLQ11cym3PK/view?usp=sharing

1 answer

Log in to vote
0
Answered by 4 years ago

You will need to pass the part hit from the client to the server, and have the server then do damage to the Character's humanoid. You use Remote Events/Functions to accomplish this.

If security is not a concern of yours (you're just experimenting or learning), this is a solution that works perfectly fine.

If you want security, the raycasting is also going to have to be handled by the server.

Any questions? Feel free to ask here or on discord.

0
Any videos or anything you can link me for the server side raycasting and kills? techN0logics 40 — 4y
0
Here is a video on remotes: https://www.youtube.com/watch?v=wCr5VXJ34T4 ; As for serverside raycasting, its the same as clientside; you just have to pass the mouse.hit.p through the Remote you use. IStarConquestI 414 — 4y
0
To damage a player, all you have to do is detect if the hitpart.Parent has a Humanoid; if so, then damage that Humanoid. IStarConquestI 414 — 4y
0
Okay, Thank you! techN0logics 40 — 4y
Ad

Answer this question