I'm creating a gun. Everything works perfectly fine when you first spawn into the game. Firing, reloading, everything works just fine. When you die and respawn, nothing works.
The only reason this is is because I've made it so that you can only fire the weapon when a number of conditions are met:
if (canFire) and (canReload) and (ammo.Value > 0) and (humanoid.Health ~= 0) then
The first two conditions are checking to see if the two booleans, canFire and canReload are set to true(the only time canReload is false is when the gun is reloading and it wouldn't make sense to be able to fire while reloading). Of course you shouldn't be able to fire the weapon if it doesn't have any ammo, and of course you shouldn't be able to fire the weapon if you're dead.
What bewilders me is that upon respawning, a new instance of a humanoid is created, and the variable in the script pointing to the humanoid seems to be pointing to the previous humanoid, which no longer exists and has 0 health. An entirely new instance of the tool is being created too, meaning there's an entirely new script, and there's an entirely new variable, referencing an entirely new humanoid, and yet it still seems to be referencing the older one.
I know that the game thinks the humanoid with 100 health has 0 health because I put an else block(for the shown condition above):
else print(canFire) print(canReload) print(ammo.Value) print(humanoid.Health) end
This is what the output looks like when attempting to fire(code block because line breaks are ignored otherwise):
true true 15 0
So, canFire is true, canReload is true, ammo's value is 15, but my humanoid's health is... 0? I even typed into the command bar at the bottom of the screen in studio:
print(game.Players.Player1.Character.Humanoid.Health) --prints 100
WHAT?!! I, sincerely, am clueless as to what is occurring. Is this just a bug on Roblox's part? I think that a lot whenever something isn't working and I can't find the issue, but most of the time I turn out to have made a simple little typo. But I don't see where my negligible typo is here. I would've found it by now. I have not a clue what is causing this. I even searched around for other forum posts about this, and found none. Maybe I'm not searching hard enough. Or for some reason I'm not realizing my small little error. If I've made one, or if this is a bug on Roblox's part, someone please, impart your knowledge. I am helpless.
Maybe it's because when the player first loads into the game, for a split second their health is 0, perhaps add a CharacterAdded or something to wait until the humanoid has properly spawned in.