Help with gun script

So basically, I’m trying to make a gun script, but it won’t print the parent of hit… This script is a child of the gun (tool)

local DAMAGE = 20

script.Parent.Activated:Connect(function()
    local ray = Ray.new(script.Parent.part.CFrame.Position, script.Parent.part.CFrame.LookVector * 100)
    print(ray.Direction)
    script.Parent.Sound:Play()
    local function visualizeRay(ray)
	local part = Instance.new("Part")
	part.Size = Vector3.new(1, 1, ray.Direction.Magnitude)
	part.CFrame = CFrame.new(
		ray.Origin + ray.Direction/2, 
		ray.Origin + ray.Direction
	)
	part.Anchored = true
	part.CanCollide = false
	part.Parent = workspace
    part.Touched:Connect(function(hit)
        print(hit.Name)
        if hit.Parent:FindFirstChild("Humanoid") then
            hit.Parent.Humanoid.Health = hit.Parent.Humanoid:TakeDamage(DAMAGE)
        end
    end)
	
	coroutine.wrap(function()
		wait(5)
		part:Destroy()
	end)()
end
end)

How can I fix this?

Touched events don’t work for parts you CFrame and anchored parts, and your part is both. It would better to find the part on the ray that you created.

local hitPart = workspace:FindPartOnRay(ray)

Your ray might be hitting the gun or the player holding the gun, so you may need to use FindPartOnRayWithIgnoreList.

local hitPart = workspace:FindPartOnRayWithIgnoreList(ray, {GunHandle, Character})

Since I’m on a server script, how would you make a character variable?

Do what he said and check if the hitPart has a descendant of humanoid.

Or would I put my code in a local script, and fire remote events to the server when I need to?

This is what my script looks like:
‘’‘’

 local DAMAGE = 20
 local Handle = script.Parent.Handle

script.Parent.Activated:Connect(function()
local ray = Ray.new(script.Parent.part.CFrame.Position, script.Parent.part.CFrame.LookVector * 100)
local hitPart = workspace:FindPartOnRay(ray)
local IgnoreList = workspace:FindPartOnRayWithIgnoreList(ray, {Handle, Character})
print(ray.Direction)
script.Parent.Sound:Play()
local function visualizeRay(ray)
local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, ray.Direction.Magnitude)
part.CFrame = CFrame.new(
	ray.Origin + ray.Direction/2, 
	ray.Origin + ray.Direction
)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
part.Touched:Connect(function(hit)
    print(hit.Name)


coroutine.wrap(function()
	wait(5)
	part:Destroy()
end)()
 end)
end
end)

              '''

When the tool is equipped, it’s a child of the player’s character. You should just be able to do

character = tool.Parent

Although I’m not sure if you’ll need the ignore list. You should try without and see if anything is getting in the way. If so, just add that to the ignore list.

1 Like

It still doesn’t print the things the ray touches

Even without the Ignore list…

What is it printing, exactly? I imagine parts on the gun could be in the way, or even the part you used to v visualize the ray.

This part of the script (30 chars)

Like I said, the Touched event won’t work so you won’t see an output. You should be printing the hitPart from the raycast local hitPart = workspace:FindPartOnRay(ray)

Roblox suggests you to use WorldRoot:Raycast() instead of Ray’s and WorldRoot:FindPartOnRay()


As @MayorGnarwhal said. Touched won’t work.


So you would do

...

local raycastResult = workspace:Raycast(script.Parent.part.Position, script.Parent.part.CFrame.LookVector * 100)
-- Instead of doing ...part.CFrame.Position we directly use Position.

print(raycastResult.Instance)

...
1 Like

So all you would do is

  '''
        print(hitPart)

     '''

right?

So which line would that replace, or would it be another line of code?

IT Would replace the ray stuff

It didn’t work… This is what I tried:

          ''''
local DAMAGE = 20
local Handle = script.Parent.Handle
local char = Handle.Parent.Parent

script.Parent.Activated:Connect(function()
--local ray = Ray.new(script.Parent.part.CFrame.Position, script.Parent.part.CFrame.LookVector * 100)    
local raycastResult = workspace:Raycast(script.Parent.part.Position, script.Parent.part.CFrame.LookVector * 100)
local hitPart = workspace:FindPartOnRay(raycastResult)
local IgnoreList = workspace:FindPartOnRayWithIgnoreList(raycastResult, {Handle, char})
print(raycastResult.Direction)
script.Parent.Sound:Play()
local function visualizeRay(raycastResult)
local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, raycastResult.Direction.Magnitude)
part.CFrame = CFrame.new(
	raycastResult.Origin + raycastResult.Direction/2, 
	raycastResult.Origin + raycastResult.Direction
)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
print(raycastResult.Instance)
coroutine.wrap(function()
	wait(5)
	part:Destroy()
end)()
end
end)
              '''