After taking damage from this script the player health bar doesn’t automatically increase again and stays at the same level. This happens in Studio and in the online place. It seems like the Humanoid Health script is just freezing.
When I test touch the Shock Part and take the damage from this script and the healthbar freezes at 100-30 = 70% (as indicated in the Health Property in the Humanoid. It doesn’t start to regen.
If I then take a minute to walk over and touch another damage brick (with a completely different damage script) I get damaged there but the healthbar jumps up to where it should have been if it was regenerating from the first damage. It then starts slowly increasing again. That script is a Server script, but it references the
I’ve kinda mashed a couple of scripts together from different sources so please excuse it if it’s absolute trash. I’m new to animating and trying to control humanoids.
The animation plays properly, there are no errors. The only issue is the health bar not regenerating.
(There was an issue with this script injuring all players when only one touches the Part, but I added another check to get rid of that, which doesn’t seem right if we’re dealing with a Local player?)
Oh yeah, the script is a local script placed in StarterCharacterScripts since that’s where the documentation tutorial page told me to put it. The Animation is a child of the scrlipt.
I tried placing it in the StarterPlayerScripts as well, but then I got an errors in line 6 about an infinite yield on WaitForChild("Humanoid").
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local animation = script:WaitForChild("Animation")
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local shock = animator:LoadAnimation(animation)
local shockButton = workspace.House.ScienceLevel.Table.Shock
local deb = false
local shock = animator:LoadAnimation(animation)
shock.Priority = Enum.AnimationPriority.Action
shock.Looped = false
shockButton.Touched:Connect(function(hit)
if not deb and hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Parent.Name == player.Name then
-- if I don't check for the last one above it'll injure all players at the same time?
deb = true
print(hit.Parent.Humanoid.Parent, hit.Parent.Humanoid.Parent.Parent)
hit.Parent.Humanoid:Health -= 30 -- I tried TakeDamage here but it made no difference
shock:Play()
humanoid.WalkSpeed = 0
shock.Ended:Wait()
humanoid.WalkSpeed = 16
task.wait(3)
deb = false
end
end)
there should be some sort of server script called “Health” in your humanoid
roblox adds that script everytime, you can copy that script, insert it into startercharacterscripts and edit that
Run the game in studio and check that the server also sees your health go down.
I think what’s going on is that you changed the health on the client, and it didn’t replicate to the rest of the game. Because the server didn’t see you actually lose any health, it still thinks you’re full health, and the Health script never updates your health value if you’re already full… so the server never tells your client “hey, update that character’s health to this new value”, and they stay desynced forever.
It’s where this issue started happening. No regen for the health bar.
I tried using other Touched parts that play animations from the toolbox (don’t worry, I checked for extra scripts etc.) but they have issues like only playing half the falling animation so my character doesn’t lay flat, they end up getting stuck 45° in the air.
As you can see I’m referencing the Humanoid in the workspace that touched the Shock Part (hit).
You’re trying to do two things: play an animation and deal damage to the player. Ideally the aesthetic part of games (animations, particles etc) will be handled by the client, so you’re doing the right thing. But the game logic (dealing damage, changing a player’s score) should be handled by the server. You might be better off using an additional (serverside) script to deal damage, though the animation and the damage won’t happen at exactly the same time.
If it’s really important to you that the shock and damage happen both at once, you could either:
Have the server fire a remoteEvent on Touched, to tell the client to play the shock animation (both the damage and animation will happen at the same time, but a short while after you touched the shock part). Simpler, more consistent, recommended.
or
Have the client pre-damage their humanoid, in preparation for the server dealing the real damage (the animation will play instantly, and it’ll look like damage happens at the same time, even though the server won’t see your clientside changes of humanoid health). Smoother experience, can lead to some weird issues in specific scenarios (see below).
Two edge cases with that second solution:
If you damage yourself, and for some reason the server doesn’t update your health the way you did, and you’re full health on the server, you won’t be full health on the client anymore and it’s gonna be pretty weird to debug that.
If you damage yourself to 0 health, you will die, even if this is only done on the client (any other cases of health change done exclusively on the client would have no impact from the server’s point of view)
Thanks for the help!
Yeah, I went with 2 scripts as suggested.
One in the Shock Part to deal the damage when it’s touched and the other one in StarterCharacterScripts to play the animation when the Shock Part is touched.