Help with making a gun do damage

I want my gun to do damage to anything in front of the crosshair that isn’t blocked by anything else.
I know nothing about raycasts but here’s the gun how it looks.
(why wont my video send ill try to make it work later, its only 34 seconds)
(btw i also need help making that bullet hole i circled around in the video show up when you shoot, thanks)
I’ve looked at the DevForum and I can’t find anything.
Please help me, anything helps.

(This is localscript in the tool

local tool = script.Parent 
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gui = player.PlayerGui.GunGui
local shootsound = script["Gun Shot"]
local torso = tool.Handle
local shoot_part = player.Character.Torso
tool.Equipped:Connect(function()
	gui.Enabled = true
end)
tool.Unequipped:Connect(function()
	gui.Enabled = false
end)
tool.Activated:Connect(function()
	gui.Fired:FireServer()
	shootsound:Play()
end)
2 Likes

First, when you fire the server you can send what you hit by changing the fire line to this

gui.Fired:FireServer(mouse.Hit)

Then you can add a server script to handle that remote event and use a raycast to detect if the thing you hit was a player or not.

local tool = script.Parent
local fireRemote = – Remote Event Name

local function onFire(player, mouseHit)
local character = player.Character
if not character then return end – if it’s not a character it stops the code

local origin = tool.Handle.Position – Where the raycast starts (the gun)
local direction = (mouseHit.Position - origin).Unit * 1000 – How far the raycast goes

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character, tool}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local rayResult = workspace:Raycast(origin, direction, raycastParams) – This returns the data the raycast got

if rayResult then
local hitPart = rayResult.Instance
local hitPosition = rayResult.Position
local hitDistance = rayResult.Distance

  local hitCharacter = hitPart.Parent
  if hitCharacter and hitCharacter:FindFirstChild("Humanoid") then
  	local humanoid = hitCharacter:FindFirstChild("Humanoid")
  	humanoid:TakeDamage(25) 
  end

end
end

fireRemote.OnServerEvent:Connect(onFire) – This starts the function when the server is fired.

Not sure if this will entirely work, but it’s a good start I think.

3 Likes

it almost seems to work, but blacklist in raycasts is deprecated, is there anything else to use instead?

nevermind i changed some things in the script and it worked, thanks!

i have one last question, is there any way to make the raycast visible for a amount of time? i want it to look like a bullet when fast through the gap

Maybe try this: (add it under the if rayResult and after the hitDistance)

local rayPart = Instance.new(“Part”) – Creates a Part as the ‘bullet’
rayPart.Size = Vector3.new(0.2, 0.2, hitDistance) – I think hitDistance is from you to the target so change it to a small size like 0.2
rayPart.CFrame = CFrame.new(origin, hitPosition) * CFrame.new(0, 0, -hitDistance / 2)
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.Material = Enum.Material.Neon
rayPart.Color = Color3.fromRGB(255, 0, 0) – Red color for visibility
rayPart.Parent = workspace

-- Tweens the bullet to the hit position
local tweenInfo = TweenInfo.new(hitDistance / 300, Enum.EasingStyle.Linear) -- Adjust speed as needed
local tween = TweenService:Create(bullet, tweenInfo, {Position = hitPosition})
tween:Play()

-- Detect when the tween is complete
tween.Completed:Connect(function()
    bullet:Destroy()
    rayPart:Destroy() -- Remove the ray part after the bullet reaches the target
end)
1 Like

theres no part called bullet though, im going to make one in serverstorage and try that

i edited it and it works now, i just made it so it flashes the entire path instead of tweening it, as i wanted it to look very fast, thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.