How to disable weapon when owner is dead

  1. What do you want to achieve? A weapon that disables upon death then re enabled after respawn.

  2. What is the issue? My gun still works when character is dead

  3. What solutions have you tried so far? Dev forum

My code so far:

local maxAmmo = 10
local ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local textLabel = playerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)
script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(3)
ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if ammo > 0 and not reloading then
ammo = ammo -1
script.Parent.GunShot:Play()
if Mouse.Target.Parent:FindFirstChild(“Humanoid”) then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)

		end
	elseif reloading == false then
		reload()
		script.Parent.GunShot:Stop()
	end
	while wait() do
		textLabel.Text = (ammo).. "/".. maxAmmo
	end
end)
local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
		reload()
	end
end)

end)

I tried putting this into the code
if script.Parent.Parent.Parent.Humanoid.Health < 0 then script.Parent.Enabled = false
wait(6)
script.Parent.Enabled = true

wouldn’t work because it got confused and thought the parent was the workspace. Please help ASAP thank you!

I have to go to that person replying, please leave it below Ill get back to you as soon as possible. SORRY

There’s a few problems that you have besides the question at hand:
1. If you are attempting to disable the script, you would do script.Enabled = false, as script refers to the script object itself.
2. Your weapon is automatically deleted and restarted when the player is given a new tool on spawn.
3. While disabling the script may work (unsure), disabling the script seems untasteful and it’d probably just be better to check when you attempt to fire if the player is dead when the player is attempting to reload.
4. If your code reaches the while wait() do block, your code will loop there permanently,

Once you fix this issue, just keep trying. It takes a lot of bad code to be able to write good code, and a simple gun system is a great way to start off (and how I started off myself!) :slightly_smiling_face:

1 Like

I think this could be solved with a disconnect. Something like this. (untested)

local connection = input.InputBegan:Connect(function(Key)
   if not player.Character.Humanoid or player.Character.Humanoid.Health <=0 then 
      connection:Disconnect()
   end
	if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
		reload()
	end
end)

Thanks for the tips but what I did in the code was if you fired it the mouse would be disabled and it wouldn’t work no matter how many times you tried do you know how to fix this?

You can see the error at the bottom and the gun no longer works

This is why I tried to disable the weapon/

I am assuming that line 18 is “if Mouse.Target.Parent …”
The problem is that Mouse.Target will not exist if the player is aiming at the sky (or if they are aiming at something that is extremely far away.) You need to check to make sure that Mouse.Target exists.
To accomplish this, you could change this line to:

if Mouse.Target and Mouse.Target.Parent:FindFirstChild("Humanoid") then
1 Like

This works but now the ammo loading screen is stuck how do you fix this?

I can’t actually see your code properly due to how you posted it, but you should remove

	while wait() do
		textLabel.Text = (ammo).. "/".. maxAmmo
	end

and instead just do

textLabel.Text = (ammo).. "/".. maxAmmo

every time you update your ammo.

1 Like

Still the same error

local maxAmmo = 10
local ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText")
script.Parent.Equipped:Connect(function(Mouse)
	local function reload()
		reloading = true
		wait(3)
		ammo = maxAmmo
		reloading = false
	end
	script.Parent.Activated:Connect(function()
		if ammo > 0 and not reloading then
			ammo = ammo -1
			script.Parent.GunShot:Play()
			if Mouse.Target and Mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)

			end
		elseif reloading == false then
			reload()
			script.Parent.GunShot:Stop()
		end
		textLabel.Text = (ammo).. "/".. maxAmmo
	end)
	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
			reload()
		end
	end)
end)

here is the code and sadly it still doesn’t work

What specifically isn’t working? What output do you have?

No errors and no warnings in the output. The only thing not working is the ammo counter/stats which is frozen at 10/10