Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make my gun stop working after death?

I'm currently working on an fps and when I test it with my friends we found a bug where you can still use the gun after you have died. So I need the gun to stop working once your health reaches 0 and it needs to start working again once you respawn.

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(2)
		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, 35)
			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()
			local Anime = script.Animation
			Anime:Play()
		end
	end)
end)

1 answer

Humanoid of player's character has an event called Died, you can connect it to a function and disable the gun every time it fires. Example:

local gunEnabled = true
local humanoid = player.Character.Humanoid

humanoid.Died:Connect(function()
	gunEnabled = false
end)

To enable it after respawning, player has an event called CharacterAdded, it fires every time the character is added.

player.CharacterAdded:Connect(function(character)
	-- character in this case is the new character that has been added
	gunEnabled = true
end)

3 comments

0

It didnt work

0

it is not supposed to work, it is supposed to show you how to do it. I made code example of how you could make fix for your gun.

0

i know but it still didnt work

This discussion is preserved as it was. New answers, comments, and votes are closed.