Issue: I have a gun system I made. When I equip a gun, it works perfectly, and I can see the GUIs. When I unequip a gun, I can't see the GUIs, which is functioning as intended. However, when I die and equip the gun again, the GUIs no longer appear. I want the GUIs the appear again when the player equips a gun, even when the player dies.
Script:
WeaponScript (ModuleScript):
local gunScripts = {} gunScripts.PistolScript = function(GunName) local replicatedStorage = game:GetService("ReplicatedStorage") local runService = game:GetService("RunService") local player = game.Players.LocalPlayer local playerGui = player:WaitForChild("PlayerGui") local gun = player:WaitForChild("Backpack"):WaitForChild(GunName) or player.Character:WaitForChild(GunName) or workspace:WaitForChild("Baseplate"):WaitForChild(GunName) or replicatedStorage:FindFirstChild(GunName, true) local mouse = player:GetMouse() local rs = game:GetService("ReplicatedStorage") local weaponInfo = require(rs.Modules.WeaponInfo) local gunIndex = weaponInfo:GetWeaponByName(GunName) local ammoAmount = gunIndex.Ammo local firing = false local reloading = false local isUnequipped = false local isReloadingStopped = false local UIS = game:GetService("UserInputService") local firerate = gunIndex.Firerate local ammoGui = playerGui:WaitForChild("AmmoGUI") local ammoText = ammoGui.Background.AmmoText local gunLabel = ammoGui.GunLabel local ReloadGui = playerGui:WaitForChild("ReloadGui") local debounce = false local mobileReloadGui = playerGui:WaitForChild("MobileReloadGui") local mobileReloadButton = mobileReloadGui:WaitForChild("ReloadButton") local GunAnimations = rs.GunAnimations local reloadText = ReloadGui.ReloadText local globalChar = player.Character or player.CharacterAdded:Wait() local globalHum = globalChar:FindFirstChild("Humanoid") local globalAnimator = globalHum:FindFirstChild("Animator") function gunScripts.loadAnimation(animator, humanoid, animation) if not animator then animator = Instance.new("Animator") animator.Parent = humanoid end local animTrack = animator:LoadAnimation(animation) return animTrack end --When the gun is equipped then, gun.Equipped:Connect(function() --Variables local re = rs:WaitForChild("Events"):WaitForChild("ShootEvent") local char = player.Character or player.CharacterAdded:Wait() local hum = char:FindFirstChild("Humanoid") local animator = hum:FindFirstChild("Animator") globalChar = char globalHum = hum globalAnimator = animator --The display text for the gunGUI displays the gun's name. gunLabel.Text = gun.Name --Allows the player to see the ammoGUI ammoGui.Enabled = true isUnequipped = false --The equip animation. local equipAnim = GunAnimations:WaitForChild("PistolEquip") local equipAnimTrack = gunScripts.loadAnimation(animator, hum, equipAnim) equipAnimTrack.Looped = false equipAnimTrack:Play() gun:SetAttribute("PlayedEquipAnimation", false) equipAnimTrack.Ended:Connect(function() gun:SetAttribute("PlayedEquipAnimation", true) end) local holdAnim = GunAnimations:WaitForChild("PistolHold") local holdAnimTrack = gunScripts.loadAnimation(animator, hum, holdAnim) holdAnimTrack.Priority = Enum.AnimationPriority.Action holdAnimTrack.Looped = true holdAnimTrack:Play() --When the gun is unequipped, gun.Unequipped:Connect(function() firing = false gun:SetAttribute("PlayedEquipAnimation", false) --disable the ammoGUI ammoGui.Enabled = false --disable the reloadGUI ReloadGui.Enabled = false --If the animation is still playing after the gun is unequipped, if equipAnimTrack.IsPlaying then --Stop the track equipAnimTrack:Stop() --Restart the time position. equipAnimTrack.TimePosition = 0 end holdAnimTrack:Stop() holdAnimTrack.TimePosition = 0 --Set playedequipanimation to false gun:SetAttribute("PlayedEquipAnimation",false) end) --When activated, it will fire remote event. function gunScripts.onActivated() --If the equip animation is done and if the player has more than 0 ammo then... if gun:GetAttribute("PlayedEquipAnimation") and ammoAmount > 0 and not reloading then --The shooting animation for the gun. local shootAnim = GunAnimations.PistolShoot --If the character's humanoid does not have a humanoid to load animations in then: local shootTrack = gunScripts.loadAnimation(animator, hum, shootAnim) shootTrack:Play() --Sets the target of the player's gun to the mouse's target. local params = RaycastParams.new() params.FilterDescendantsInstances = { gun, player.Character } params.FilterType = Enum.RaycastFilterType.Exclude params.IgnoreWater = true local mouseRay = game.Workspace.Camera:ScreenPointToRay(mouse.X, mouse.Y) local origin = mouseRay.Origin local direction = mouseRay.Direction * 500 local result = workspace:Raycast(origin, direction, params) local target = nil if result and result.Instance then target = result.Instance end --Fires a remote event to damage the enemy. re:FireServer(target, result) ammoAmount -= 1 --If the ammo amount is 0, however, then the reloadGUI displays. end end end) --If the gun is activated, connect to a function mouse.Button1Down:Connect(function() if not firing and gun:GetAttribute("PlayedEquipAnimation") and UIS.KeyboardEnabled and not reloading and ammoAmount > 0 then firing = true while firing == true do gunScripts.onActivated() wait(firerate) end end end) gun.Deactivated:Connect(function() if UIS.KeyboardEnabled then firing = false end end) UIS.InputBegan:Connect(function(input, GPE) if not GPE then if input.UserInputType == Enum.UserInputType.Touch and not UIS.KeyboardEnabled then if not firing and gun:GetAttribute("PlayedEquipAnimation") and not reloading then firing = true while firing == true do gunScripts.onActivated() wait(firerate) end end end end end) UIS.InputEnded:Connect(function(input, GPE) if not GPE then if input.UserInputType == Enum.UserInputType.Touch and not UIS.KeyboardEnabled then firing = false end end end) local reloadDebounce = false UIS.InputBegan:Connect(function(input, GPE) if not GPE then if input.KeyCode == Enum.KeyCode.R and ammoAmount < gunIndex.Ammo and gun:GetAttribute("PlayedEquipAnimation") and UIS.KeyboardEnabled and reloadDebounce == false and reloading == false then reloadDebounce = true reloading = true local reloadAnim = GunAnimations:WaitForChild("PistolReload") local reloadTrack = gunScripts.loadAnimation(globalAnimator, globalHum, reloadAnim) reloadTrack:Play() local reloadSound = gun:WaitForChild("ReloadSound") reloadSound:Play() gun.Unequipped:Connect(function() isUnequipped = true reloading = false ReloadGui.Enabled = false if reloadTrack.IsPlaying then for i,v in pairs (globalAnimator:GetPlayingAnimationTracks()) do if v.Name == "PistolReload" or v.Name == "PistolHold" or v.Name == "PistolEquip" or v.Name == "Shoot" then v:Stop() isReloadingStopped = true end end reloadSound:Stop() reloadDebounce = false end end) if isUnequipped then return end reloadTrack.Ended:Connect(function() if not isUnequipped then reloading = false reloadDebounce = false ammoAmount = gunIndex.Ammo ReloadGui.Enabled = false end end) end end end) if not UIS.KeyboardEnabled then gun.Equipped:Connect(function() if ammoAmount < gunIndex.Ammo then mobileReloadGui.Enabled = true end end) gun.Unequipped:Connect(function() mobileReloadGui.Enabled = false end) mobileReloadButton.MouseButton1Down:Connect(function() if reloadDebounce == false then reloadDebounce = true local reloadAnim = GunAnimations:WaitForChild("PistolReload") local reloadTrack = gunScripts.loadAnimation(globalAnimator,globalHum, reloadAnim) reloadTrack:Play() local reloadSound = gun:WaitForChild("ReloadSound") reloadSound:Play() reloading = true gun.Unequipped:Connect(function() isUnequipped = true if reloadTrack.IsPlaying then for i,v in pairs (globalAnimator:GetPlayingAnimationTracks()) do if v.Name == "PistolReload" then v:Stop() end end reloadSound:Stop() reloading = false reloadDebounce = false end end) if isUnequipped then return end reloadTrack.Ended:Connect(function() if not isUnequipped then reloading = false ammoAmount = gunIndex.Ammo ReloadGui.Enabled = false reloadDebounce = false ammoText.Text = "Ammo: "..ammoAmount.."/"..gunIndex.Ammo end end) end end) end runService.Heartbeat:Connect(function() if ammoAmount == 0 and not isUnequipped then ReloadGui.Enabled = true end if not UIS.KeyboardEnabled then if not isUnequipped and ammoAmount < gunIndex.Ammo then mobileReloadGui.Enabled = true elseif isUnequipped and ammoAmount < gunIndex.Ammo then mobileReloadGui.Enabled = false end if ammoAmount == gunIndex.Ammo then mobileReloadGui.Enabled = false end end ammoText.Text = "Ammo: "..ammoAmount.."/"..gunIndex.Ammo end) end return gunScripts
Conclusion: Sorry if my code is a bit messy, hope people can see it. I removed the other scripts for the sake of relevance and length. I also had to remove a "Context" part of the post, apparently because it exceeded 10,000 characters, even if the character amount was clearly below that amount. As always, thank you for taking the time to read this.