What do you want to achieve? A weapon that disables upon death then re enabled after respawn.
What is the issue? My gun still works when character is dead
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!
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!)
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?
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
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)