#Need help with this raycasting damage gun

14 messages · Page 1 of 1 (latest)

fickle tinsel
#

its because you're changing the health of the enemy on the client. You need to make some sort of remote event that you can fire whenever you shoot the gun which will pass the mouse hit position, so then a server script can do the raycast and deal damage to the enemy

dry meadow
#

thats the thing I don't know how to do

fickle tinsel
#

A remotevent is an instance. You can create a new remote event in the replicatedStorage and call it something like "shootEvent". Then, create a server script in serverScriptService (doesn't have to be in sss but it help keep organized) that will connect a function to whenever the remoteEvent is fired and perform the raycast and damage stuff.

#

Or just follow these instructions cuz im bored:

#
  1. Create a remote event in ReplicatedStorage called "ShootEvent"
#

2.Create a script in ServerScriptService and paste the following code (its pretty much just ur code)

local shootEvent = game.ReplicatedStorage.ShootEvent 

shootEvent.OnServerEvent:Connect(function(player, tool, hit) --when calling a remote event from the client, it always passes the player
    local ray = Ray.new(tool.Handle.CFrame.p, (hit.p - tool.Handle.CFrame.p).unit * 400)
    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.45
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (tool.Handle.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.02, 0.02, distance)
    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(-0.15, 0.2, -distance / 2)

    game:GetService("Debris"):AddItem(beam, 0.1)
    if part then
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if not humanoid then
            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
        end

        if humanoid then
            humanoid.Health -= 25
        end
    end
end)
#

3.Put this code in the localscript in ur gun (all the stuff it did is now handled by the server, and it simply fires a remote event with the tool instance and mouse hit position)

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local shootEvent = game.ReplicatedStorage.ShootEvent

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

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!")
        shootEvent:FireServer(tool, mouse.Hit)
    end)
end)
dry meadow
#

ok

dry meadow
#

ty

#

omg tysm

fickle tinsel
dry meadow
#

it worked

#

omg

#

I have been trying to fix for literally 2 hours