I’m trying to script a part that deals damage to a player unless a boolvalue in the player (called ‘healthenabled’) is set to true. Additionally, when this boolvalue is set to true, the players maxhealth and health are both set to math.huge.
But no matter what I try nothing seems to prevent the damage! I’ve tried rewriting the script multiple times but I’m pretty sure I’m making a really simple mistake somewhere.
I’ve tested all of these out in studio and even though healthenabled is true and maxhealth + health are set to inf, my character still dies. I’d really appreciate any help!
Here are some of the scripts I tried:
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
local bodyColors = hit.Parent:FindFirstChildOfClass("BodyColors")
if hum and bodyColors then
local maxHealth = hum.MaxHealth
if maxHealth == 100 then
hum.Health = hum.Health - 40
end
end
end)
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
local bodyColors = hit.Parent:FindFirstChildOfClass("BodyColors")
if hum and bodyColors then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local healthEnabledValue = player:FindFirstChild("healthenabled")
if healthEnabledValue and healthEnabledValue.Value == false then
hum.Health = hum.Health - 40
end
end
end
end)
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hit.Parent:FindFirstChildOfClass("BodyColors") then
local boop = game.Players:FindFirstChild(hit.Parent.Name)
if boop then
local ble = boop:FindFirstChild("healthenabled")
if ble and ble.Value == false then
hum.Health = hum.Health - 40
end
end
end
end)
Are you sure you’ve set the ‘healthenabled’ value to true while testing? Nothing seems wrong with the given code. If the ‘healthenabled’ value is true, then there might be some other code within the script (or any other script) that could also be damaging the player.
100%! I’ve looked around at other scripts and didn’t find anything, but I really don’t see how they could possibly affect this. It’s a simple part containing just that 1 script.
First off you should use Humanoid:TakeDamage(40) instead.
Second, if you use TakeDamage then you can use the force field instance to prevent damage.
Third and the most important, DO NOT use math.huge on those values, it is the absolute biggest value possible and it really could really mess up the calculation on the core script side(I suggest 1e6)
Is the code that you have given in a Server Script or LocalScript? And are you changing the ‘healthenabled’ value in the explorer tab while testing in studio?
If the given code is in a Server Script and you are changing the value from the explorer tab, then that is the reason why nothing is seemingly preventing the damage. This is because when you are changing the value from the Explorer tab, the change is only detected on the client, not on the server.
A better way to change the ‘healthenabled’ value is to use the Developer Console while testing in game by pressing F9 and going to the Server tab, then executing the following code line:
It’s in a server script inside the part. healthenabled is either true or false based on whether the player chooses to enable it if they have a gamepass.
I messed around and managed to get it to work by setting the scripts RunContext to Client so thank you for reminding me about the the whole client/server stuff