Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my script saying that the Humanoid's health is 0 when it isn't?

Asked by
Rinpix 639 Moderation Voter
4 years ago

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.

0
Check if the health is greater than 0. :) Hydrogyn 155 — 4y
0
The issue isn't how I'm writing my conditional statements, it's that two different scripts are getting two different values from the same property of the same instance. Rinpix 639 — 4y
0
I mean, I put an else block and it literally prints the humanoid's health and says it is 0 when it is not. Rinpix 639 — 4y
0
maybe something to do with you printing the health from the server but the client says its 100 or killing the npc from client whereas on the server the health is 100 greatneil80 2647 — 4y
View all comments (15 more)
0
To explain in full, I have a LocalScript inside of the tool. It controls whether or not the client can fire the remote event used to communicate with the server based on the four conditions in my initial post, meaning it's on the client side. The client, using that script checks to see if the humanoid's health is 0 or not. If it isn't, allow the client to fire the remote event. Rinpix 639 — 4y
0
just put a greater than sign. Hydrogyn 155 — 4y
0
^ Tried that and it still doesn't work. Rinpix 639 — 4y
0
The gun works perfectly fine the first time I spawn in. When I die, I respawn, and am given a new humanoid, a new gun(starterpack), and a new localscript, with a new variable referencing the new humanoid. The new localscript tells me that the new humanoid has 0 health, despite the fact that my character is alive and well. I go down into the command bar and print my character's health. Rinpix 639 — 4y
0
The output prints 100. So no, it doesn't have anything to do with the use of the inequality operator or the use of the greater than operator, or whether or not the server is printing the health(because I played solo, commands entered into the command bar as the player are client-sided). I'm almost certain it's just a bug. Rinpix 639 — 4y
0
If two different localscripts are referencing the same instance, and the same property, why are they printing different values? Rinpix 639 — 4y
0
Create a .Died function so it changes a bool value to true or false. Hydrogyn 155 — 4y
0
I even tried creating a variable that refers to the humanoid's health and creating an event listener with the RenderStepped event of RunService and updating the variable. It still didn't work. Idk what it is. Rinpix 639 — 4y
0
I tried using .Died as well. Still didn't work. I have no clue what's happening. Rinpix 639 — 4y
0
Is there a localscript located in the StarterPlayer or just in the gun? Hydrogyn 155 — 4y
0
Because if its in the StarterPlayer it only executes once. Hydrogyn 155 — 4y
0
The LocalScript is inside of the tool, as stated earlier. I really don't know what's going on. Rinpix 639 — 4y
0
Ok, so when I used the .Died event of the humanoid to alter a boolean value, it meets all of the conditions. But then it takes us to a more complicated part this problem. You can't shoot through walls with raycasting, right? Well, that's true until the gun itself is sticking through a wall. To combat this, I cast a ray on the client from the barrel(situated at the end of the gun and held in place Rinpix 639 — 4y
0
with a WeldConstraint) to the client's HumanoidRootPart(with the character being on the IgnoreDescendants list), and if there's a part, that means that there's something between the barrel and the HumanoidRootPart, meaning they're attempting to wallshoot. If there isn't a part then they can fire the weapon. Rinpix 639 — 4y
0
The ray is cast, and it tells me that it hit a part, UpperTorso. Okay, but did I not put the character in the IgnoreDescendants list? I'm literally playing solo. Who else's UpperTorso could I have hit? There aren't any NPCs either. It's just me and a baseplate. I go to check my code, and I did indeed add my character to the IgnoreDescendants list. My variables are referencing the correct instances Rinpix 639 — 4y

1 answer

Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago

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.

0
By default, the humanoid's health property is 100. So it's not 0 for a split second. I also am checking for its health many seconds after I spawn, so it's not that either. Rinpix 639 — 4y
Ad

Answer this question