Help scripting a gun

I need help scripting my gun because all of my code is not working and i cant find the issue
can someone help me?

1 Like

hears my main script

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*300,RaycastParams)

if raycastResult then
	local hitpart = raycastResult.Instance 
	local model = hitpart:FindFirstAncestorOfclass("model")
	
	if model then
		if model:FindFirstChild("Humanoid") then
			model.Humanoid.Health -= 30
		end
	end
end

end)

1 Like

hears my shooting script

local mouse = game.Players.LocalPlayer:GetMouse ()

mouse.Button1Down:Connect(function()

script.Parent.Fire:FireServer(mouse.Hit.p)

end)

Can you perhaps describe in greater detail what the issue is? Is the gun dealing damage? Are you getting any errors?

Btw, I noticed your script doesn’t check if the gun is actually equipped when you click. Unless your gun is ALWAYS equipped, you might want to change that.

Edit:

I think your issue might actually be caused by you not checking for the humanoid instance correctly.

if raycastResult then
	local hitpart = raycastResult.Instance 
	local model = hitpart:FindFirstAncestorOfclass("model")
	
	if model then
		if model:FindFirstChild("Humanoid") then
			model.Humanoid.Health -= 30
		end
	end
end

The ray would most likely be hitting a bodypart, like the torso, and not the actual model. So what you instead should do, is find the Humanoid in the parent of the bodypart, as that is where it will be located:

if raycastResult then
	local hitpart = raycastResult.Instance 
	local model = hitpart.Parent
	
	if model then
		if model:FindFirstChild("Humanoid") then
			model.Humanoid.Health -= 30
		end
	end
end
```

so whats wrong is that my gun is doing nothing at all and i have no idea how to fix that

is this the whole script? and where is this script located, along with the local script?

the longer one is the server script

ill try that really quick thank you

theres a red line under the end

1 Like

ur forgot to add the last end)

1 Like

i already tried that it still had the red line

add end) at the last line and there wont be that red line

thats what i did and there still a red line

ok i see what you meant i did it and that worked thank you

1 Like

my gun still does not shoot and all of the script is right

as what @kosava mentioned above, you should make an equipped and unequipped function to check if you have a gun or not. Maybe that’ll work

i can pull out the gun but it is invisible in my inventory and also i have no idea how to make one of those functions yet