Assistance in fixing a gun script

I am trying to set up a basic gun base, for later development, but it isn’t doing damage to the dummy that i placed, both from behind and in front of a wall.

Everything works fine, value being changed when it needs to, and the sound is played. The only issue is damaging the dummy, and the Raytracing

The gun is this, and the green circle is the attachment
image

This is the setup for the testing
image

I tried changing the attachments, changing from attachment.CFrame.Position to Attachment.WorldCFrame.Position, and changing Raycast Parems

Here is the Client Side Script:

local player = game.Players.LocalPlayer
local tool = script.Parent

tool.Activated:Connect(function()
	if tool.Parent == player.Character then
		local mouse = player:GetMouse()
		if mouse.Target then
			script.Parent.Hit:FireServer(mouse.Hit.Position, mouse.Target)
		end
	end
end)

Here is the Server side script:

local event = script.Parent.Hit
local playerservice = game.Players

event.OnServerEvent:Connect(function(player, Cframe, target)
	script.Parent.canfire.Value = false
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.FilterDescendantsInstances = {player.Character}
	local origin = script.Parent.Handle.FirePoint.CFrame.Position
	local result = game.Workspace:Raycast(origin, Cframe-origin, rayParams)
	local distance = (origin - Cframe).Magnitude
	if result ~= target then
		script.Parent.Handle.Fire:Play()
		wait(1.2)
		script.Parent.canfire.Value = true
	else
		if playerservice:GetPlayerFromCharacter(target.Parent) then
			if target.Name == "Head" then
				script.Parent.Handle.Fire:Play()
				target.Parent.Humanoid:TakeDamage(20)
				wait(1.2)
				script.Parent.canfire.Value = true
			else
				script.Parent.Handle.Fire:Play()
				target.Parent.Humanoid:TakeDamage(5)
				wait(1.2)
				script.Parent.canfire.Value = true
			end
		else
			script.Parent.Handle.Fire:Play()
			wait(1.2)
			script.Parent.canfire.Value = true
		end
	end
end)
1 Like

You should check out the FastCast module.

How I use it is like this:

Remote.OnServerEvent:Connect(function(Player: Player, Position: Vector3, OriginPosition: Vector3, IsShotgun: boolean, FireSound: Sound)
	if Tool.Parent ~= Player.Character or Player.Character:FindFirstChildOfClass("ForceField") then return end

	-- For gun shoot effect
	--[[for _, Object in Tool.Handle.Barrel:GetChildren() do
		if Object:IsA("ParticleEmitter") then
			Object:Emit()
		end

		if Object:IsA("PointLight") then
			Object.Enabled = true

			task.delay(0.6, function()
				Object.Enabled = false
			end)
		end
	end]]

	local Direction = (Position - OriginPosition).Unit

	local Cast = BulletCaster:Fire(OriginPosition, Direction, GunData.Velocity/10, BulletBehavior)
	Cast.RayInfo.CosmeticBulletObject:SetAttribute("Player", Player.Name)
	FireSound:Play()
end)

As for the event being fired this is what I do:

self.ShootEvent:FireServer(Mouse.Hit.Position, self.Tool.Barrel.Position, self.IsShotgun, self.Fire)

You can handle the ray hitting using FastCast’s RayHit event.
https://etithespir.it/FastCastAPIDocs/fastcast-objects/caster/#rbxscriptsignal-rayhitactivecast-raycastresult-vector3-instance

Hi superglitchywilliam2, I think the direction parameter you put in is wrong, it should be done like this;

	local result = game.Workspace:Raycast(origin, (Cframe.Position-origin).Unit, rayParams)

The entire script:

local event = script.Parent.Hit
local playerservice = game.Players

event.OnServerEvent:Connect(function(player, Cframe, target)
	script.Parent.canfire.Value = false
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.FilterDescendantsInstances = {player.Character}
	local origin = script.Parent.Handle.FirePoint.CFrame.Position
	local result = game.Workspace:Raycast(origin, (Cframe.Position-origin).Unit, rayParams)
	local distance = (origin - Cframe).Magnitude
	if result ~= target then
		script.Parent.Handle.Fire:Play()
		wait(1.2)
		script.Parent.canfire.Value = true
	else
		if playerservice:GetPlayerFromCharacter(target.Parent) then
			if target.Name == "Head" then
				script.Parent.Handle.Fire:Play()
				target.Parent.Humanoid:TakeDamage(20)
				wait(1.2)
				script.Parent.canfire.Value = true
			else
				script.Parent.Handle.Fire:Play()
				target.Parent.Humanoid:TakeDamage(5)
				wait(1.2)
				script.Parent.canfire.Value = true
			end
		else
			script.Parent.Handle.Fire:Play()
			wait(1.2)
			script.Parent.canfire.Value = true
		end
	end
end)

Using the FastCast module is useful too, but hopefully this amendment alone should solve your current issue, let me know of any further problems.

I ended up figuring it out by using the handle instead of an attachment. Thanks for the suggestions though. FastCast looks great, but I don’t really think it’s for me.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.