Viewmodel Shooting Issue

Hello! I’ve been trying to make a Viewmodel Shooter Game, the issue is that I made a script that doesn’t work, and I can’t quite tell what’s causing the script to malfunction…


The thing I want to achieve is that, if the cursor is pointing at a player or an NPC, it will damage the humanoid of the player/NPC. For some reason this doesn’t work, I’ve tried with MousePos, but it gave me an odd error

game.ReplicatedStorage.OnFire.OnServerEvent:Connect(function(player)
	local endpart = game:GetService("ReplicatedStorage").Endpart.Value
	local shootpart = game:GetService("ReplicatedStorage").Shootpart.Value
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(shootpart.Position, (endpart.Position - shootpart.Position)*25, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")

		if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -=5
			end
		end
	end
end)

This is the code that I made, I made two object values that starts from a point and end to another point, for some reason this doesn’t work.

If someone can find the issue it would be appreciated!

If you’re setting the Endpart and Shootpart from the client the server won’t be able to read those values, you could pass the positions in the onfire event

Hmmm… wouldn’t there be another way? Like with MousePos, even though I tried it

You could send the mousepos in the event and maybe raycast from the players head if you only wanna pass one value

game.ReplicatedStorage.OnFire.OnServerEvent:Connect(function(player,MousePos)
	local Character = player.Character
	local Head = Character:FindFirstChild('Head')
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(Head.Position, (MousePos - Head.Position)*25, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")

		if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -=5
			end
		end
	end
end)

image
This error appears, at line 8

What does your FireServer look like on the local script

local function fire()
	if (tick() - LastFired) < (1 /(RPM / 60)) then
		return
	end

	clicks +=1
	script.Shoot:Play()
	if clicks >=30 then
		script.Shoot:Stop()
		script.NoAmmo:Play()
	else
		ReloadAnimationTrack:Stop()
		ShootAnimationTrack:Play()
		game.ReplicatedStorage.OnFire:FireServer()
	end

	LastFired = tick()
end

All the scripts are in StarterCharacterScripts, would that be a problem?

Try doing this if you have mouse declared, and the script is fine in startercharacterscripts

game.ReplicatedStorage.OnFire:FireServer(mouse.Position)

That seems to work! Thanks for the help!

1 Like

No problem! Gun scripts are half of what I look at on Roblox lol

1 Like