Gun not shooting

hello, im trying to make a simple gun.
but its not shooting.
can you guys help?
i have a localscript, server script, remotevent.
localscript code:

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

script.Parent.Activated:Connect(function()
	
end)

server script code:

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
	local raycastParams = RaycastParams.new()
	RaycastParams.FilterDescendantInstances = {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)

remote event name: Fire
Localscript name: Client
server Script name: Server.

1 Like

have you fired the serverevent from the localscript?

1 Like

no, how do i do that? im new so i dont understand it that good.

1 Like

just reference the event in the local script and do
event:FireServer()

1 Like

Try this :

local RemoteEvent = game:GetService("ReplicatedStorage").Fire
local mouse = game.Players.LocalPlayer:GetMouse()

script.Parent.Activated:Connect(function()
	RemoteEvent:FireServer()
end)
1 Like

So,

I made some changes to the RemoteEvent:

  • Variables are Abbreviated for Easy typing

  • Changed Ray cast Origin to the Players PrimaryPart
    the reason for this is so when firing the gun at close range, it will actually still detect the Humanoid instead of going though it

script.Parent.Fire.OnServerEvent:Connect(function(Plr,MP)
	local RCP = RaycastParams.new()
	RCP.FilterDescendantInstances = {Plr.Character}
	RCP.FilterType = Enum.RaycastFilterType.Blacklist
	
	local RCR = workspace:Raycast(Plr.Character.PrimaryPart.Position, (MP - Plr.Character.PrimaryPart.Position)*300, raycastParams)
	
	if raycastResult then
		local HPart = RCR.Instance
		local M = HPart:FindFirstAncestorOfClass("Model")
		
		if M then
			if M:FindFirstChild("Humanoid") then
				M.Humanoid:TakeDamage(30)
			end
		end
	end

As Stated Above by @FireStrykerAzul, Do this:

Tool = script.Parent
Fire = Tool.Fire
 -- Script.Parent = Tool
Mouse = game.Players.LocalPlayer:GetMouse()

script.Parent.Activated:Connect(function()
	Fire:FireServer(Mouse.Hit.p)
end)

I am also aware of where this:

2 Likes

yes that is alivin blox i used him to make a gun but it does not work.

btw my model is actually a union and in the script says something about a model. does it have to be a model or the name?

Looking at the script it has to be a tool

1 Like

There’s nothing being done here. You’re just initializing the Activated function. Do:

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

script.Parent.Activated:Connect(function()
	script.Parent.Fire:Fire(game.Players.LocalPlayer, mouse.Hit)
end)

That makes code way less readable and editable for the future.

It Really doesnt tho. :confused:
Its a common thing developers do

1 Like

When you initialize variable you make the script read faster and and it is just 1000x more convenient than put the entire way to get the thing !
So he is good !
And you should use variable for the future ! :wink:
Have a nice day and sorry for my bad english !

1 Like

Can you show us your explorer (where your script are and the remote event) ?