Help with a gun script I made

No errors, and I put where the local script is and where the serverscript is here:

-- local script
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local Player = game.Players.LocalPlayer
local Character = Player.Character

script.Parent.Activated:Connect(function()
   local raycastParams = RaycastParams.new()
   raycastParams.FilterDescendantsInstances = {SHOOTY_PART.Parent}
   raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
   local raycastResult = workspace:Raycast(SHOOTY_PART.Position, SHOOTY_PART.CFrame.LookVector * 50, raycastParams)
   script.Parent.Handle.Sound:Play()
     local hitPart = raycastResult.Instance
    print("Instance created")
game.ReplicatedStorage.Shooty:FireServer(Character, HEADSHOT, BODY_DAMAGE, hitPart,raycastResult )
end)
-- server script
game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(Character, HEADSHOT, BODY_DAMAGE, hitPart, raycastResult)
       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("part created")
    if raycastResult then
       if hitPart.Parent == Character then
          if hitPart.Name == "Head" then
             Character.Humanoid:TakeDamage(HEADSHOT)
             print("Headshot")
          else
             Character.Humanoid:TakeDamage(BODY_DAMAGE)
             print("Character")
          end
       end
    end
    end 
end)

Well I would cast the ray on the server rather than the client, it would be easier overall to do it on the server. This could also cause issues and is probably why you’re getting this problem.

So put this code (from the LocalScript) in the ServerScript

local raycastParams = RaycastParams.new()
   raycastParams.FilterDescendantsInstances = {SHOOTY_PART.Parent}
   raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
   local raycastResult = workspace:Raycast(SHOOTY_PART.Position, SHOOTY_PART.CFrame.LookVector * 50, raycastParams)
   script.Parent.Handle.Sound:Play()
     local hitPart = raycastResult.Instance

Then once you done that I would point the ray’s direction to the mouse’s position. So in the LocalScript under local Player = game.Players.LocalPlayer put local Mouse = Player:GetMouse() this will get the mouse so you can access it’s properties. Once you’ve done that, under the
script.Parent.Activated:Connect(function()
put this line:

local MousePos = Mouse.Hit.p

and then send over the MousePos in the RemoteEvent in the script.Parent.Activated:Connect(function()

Once you’ve done all of that, in the ServerScript put this line:

raycastResult = workspace:Raycast = (Ray's origin), (MousePos - (Ray's origin) .Unit * 50)

Don’t know if that will fix the issue, but it will cast the ray to the mouse’s position instead of the lookvector of whatever the ray’s origin is. Once you’ve done all of that tell me if there are any errors with what I just gave you.

Where is the serverscript would I put the top pieces of code in?

I would put it in the visualizeRay function, but put it above the part where you make the part.

no errors but it is still not working

here is the script:

-- local script in tool
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()

script.Parent.Activated:Connect(function()
local MousePos = mouse.Hit.p
game.ReplicatedStorage.Shooty:FireServer(Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART.Position, MousePos)
end)
-- ss in tool
local debris = game:GetService("Debris")
game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos)
    local function visualizeRay(raycastResult)
    local raycastParams = RaycastParams.new()
   raycastParams.FilterDescendantsInstances = {SHOOTY_PART.Parent}
   raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
   raycastResult = workspace:Raycast(SHOOTY_PART, MousePos - SHOOTY_PART.Unit * 50)
   script.Parent.Handle.Sound:Play()
     local hitPart = raycastResult.Instance
    print("Instance created")
	local part = Instance.new("Part")
    print("0")
	part.Size = Vector3.new(1, 1,raycastResult.Direction.Magnitude)
	part.CFrame = CFrame.new(
		raycastResult.Origin + raycastResult.Direction/2, 
        print("1"),
		raycastResult.Origin + raycastResult.Direction
	)
    print("2")
	part.Anchored = true
	part.CanCollide = false
	part.Parent = workspace
    debris(part, 0.3)
    print("part created")
    if raycastResult then
       if hitPart.Parent == Character then
          if hitPart.Name == "Head" then
             Character.Humanoid:TakeDamage(HEADSHOT)
             print("Headshot")
          else
             Character.Humanoid:TakeDamage(BODY_DAMAGE)
             print("Character")
          end
       end
    end
    end 
end)

Help me please

I’m really really sorry for the extremely late response, I had to go do something for a bit. But I implemented your script into one of my own gun scripts that didn’t have a part that would shoot out. I tweaked a few things and eventually got it working, here is the script:

local ray = workspace:Raycast(Barrel.Position, (mousePos - Barrel.Position).Unit * 100)
			local direction = (mousePos - Barrel.Position).Unit * 100
			local midPoint = Barrel.Position + direction/2
			local part = Instance.new("Part")
			part.Size = Vector3.new(0.2, 0.2,direction.Magnitude)
			part.CFrame = CFrame.new(midPoint, Barrel.Position)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = workspace
			part.BrickColor = BrickColor.new("New Yeller")
    		debris:AddItem(part,0.1)
   			print("part created")

You may need to tweak this a little bit to correspond it with your script, but if any issues pop up please tell me and I’ll try to fix it :slight_smile:

would I replace the serverscript code with this?

Yes (30 charrrrrrrrrrrrrrrrrrrs)

It printed an error, here is the scripts

-- local script
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()

script.Parent.Activated:Connect(function()
local MousePos = mouse.Hit.p
game.ReplicatedStorage.Shooty:FireServer(Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART.Position, MousePos)
end)
-- server script
local debris = game:GetService("Debris")
game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos)
    local ray = workspace:Raycast(SHOOTY_PART.Position, (MousePos - SHOOTY_PART.Position).Unit * 100)
			local direction = (MousePos - SHOOTY_PART.Position).Unit * 100
			local midPoint = SHOOTY_PART.Position + direction/2
			local part = Instance.new("Part")
			part.Size = Vector3.new(0.2, 0.2,direction.Magnitude)
			part.CFrame = CFrame.new(midPoint, SHOOTY_PART.Position)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = workspace
			part.BrickColor = BrickColor.new("New Yeller")
    		debris:AddItem(part,0.1)
   			print("part created")
    local hitPart = ray.Instance
    if ray then
       if hitPart.Parent == Character then
          if hitPart.Name == "Head" then
             Character.Humanoid:TakeDamage(HEADSHOT)
             print("Headshot")
          else
             Character.Humanoid:TakeDamage(BODY_DAMAGE)
             print("Character")
          end
       end
    end
end)

Whoops I forgot about the error! The error is: 08:04:43.913 - Players.Kamlkaze_Kid.Backpack.G-17.Script:36: attempt to index number with ‘Position’. I don’t know why this is happening, SHOOTY_PART is a part and position is a member of part

Again, extremely sorry for the late response. I was actually on an 8 hour drive when I responded to your question and when I got home my internet wasn’t working but I have it fixed now. So, can you highlight the line that the error is on? Even though it says on Line 36 I’m pretty sure you have a bit of code missing so I’m not sure what exact line it is on.

This line:

local ray = workspace:Raycast(SHOOTY_PART.Position, (MousePos - SHOOTY_PART.Position).Unit * 100)

I think I see the issue, you see whenever you call a RemoteEvent and contact the server from the client regardless of how many arguments you put in the :FireServer the first argument will always be the player. That probably sounds confusing so let me explain. If I were to fire a RemoteEvent like this:

local Part = game.Workspace.Part
game.ReplicatedStorage.RemoteEvent:FireServer(Part)

and then on the server script if I put:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Part)

the variable “Part” on the server script is now equal to the player that fired the event. So to fix this I would put

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, Part)

So now player is equal to the player that fired the event and Part is equal to game.Workspace.Part.

So, to fix your error on the server script, instead of having:

game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos)

replace that with this line:

game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(player, Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos)

It does create a part, but when I shoot an NPC it doesn’t damage them

Any errors when you shoot the npc?

no (30 charssssssssssssssssssss)

From the hit part = ray.Instance replace all of it with this new code:

local hitPart = ray.Instance
if ray then
	if hitPart.Parent:FindFirstChild("Humanoid") then
		if hitPart.Instance.Name == "Head" then	
			hitPart.Parent.Humanoid:TakeDamage(HEADSHOT)	
		else
			hitPart.Parent.Humanoid:TakeDamage(BODY_DAMAGE)	
		end
	end
end

No error but still not working, can someone tell me why?