Why is this gun script not working?

the title says it all.

is a localscript
script:

debounce = 0

script.Parent.Activated:Connect(function(mouse)
	if debounce == 0 then
			debounce = 1
			if mouse.Target ~= nil then
				if mouse.Target.Parent:findFirstChild("Humanoid") ~= nil and mouse.Target.Name ~= "Head" then
					mouse.Target.Parent.Humanoid:TakeDamage(100)
				elseif mouse.Target.Parent.Parent:findFirstChild("Humanoid") ~= nil then
					mouse.Target.Parent.Parent.Humanoid:TakeDamage(100)
				elseif mouse.Target.Name == "Head" then
					mouse.Target.Parent.Humanoid:TakeDamage(100)
				end
			end
			script.Parent.Hole.Flash.Enabled = true
			script.Parent.GripUp = Vector3.new(0,1,-0.1)
			wait(0.1)
			script.Parent.Hole.Flash.Enabled = false
			script.Parent.GripUp = Vector3.new(0,1,0)
			wait(0.9)
		debounce = 0
	end
end)

Just to clarify: did you mean why is this gun script not* working?

It’s just rather strange for developers to come to the ScriptingHelp section asking why scripts work :stuck_out_tongue:

1 Like

lol yes i just noticed that! :yum:

This script is either running on the server or the client (Script or LocalScript). Either way, there’s a few mistakes related to this.

  • You’d only want to use the mouse object and properties such as mouse.Target on the client.
  • Secondly, you’d only want to deal damage on the server. How else would the damage replicate?

A gun script, since it requires both input and damage, needs work on both sides of the client-server boundary. You may want to consider sending a RemoteEvent to the server from the client telling the server to deal the damage. You may also want to consider sanity checks to make it less exploitable.

1 Like

changed script to

debounce = 0

function equipped(mouse)
	function Shoot()
		if debounce == 0 then
			debounce = 1
			if mouse.Target ~= nil then
				if mouse.Target.Parent:findFirstChild("Humanoid") ~= nil and mouse.Target.Name ~= "Head" then
					mouse.Target.Parent.Humanoid:TakeDamage(50)
				elseif mouse.Target.Parent.Parent:findFirstChild("Humanoid") ~= nil then
					mouse.Target.Parent.Parent.Humanoid:TakeDamage(50)
				elseif mouse.Target.Name == "Head" then
					mouse.Target.Parent.Humanoid:TakeDamage(50)
				end
			end
			script.Parent.Hole.Flash.Enabled = true
			script.Parent.GripUp = Vector3.new(0,1,-0.1)
			wait(0.1)
			script.Parent.Hole.Flash.Enabled = false
			script.Parent.GripUp = Vector3.new(0,1,0)
			wait(0.9)
			debounce = 0
		end
	end
	mouse.Button1Down:connect(Shoot)
end

script.Parent.Equipped:connect(equipped)

it works now, but only for the first shot