Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone help me with block health?

I am working on a base that is going to have a wall that can be attacked and when its hit points hit 0 it will disappear. I have made a model and added the part and a humanoid part as well. I am not sure why the block will not disappear when its hit points hit 0. can anyone help me please...


		if Script.Parent.Humanoid.Health > 0 then do
			Script.Parent.Part.Transparency = 0
			Script.Parent.Part.CanCollide = true
			
		 if Script.Parent.Humanoid.Health <= 0 then do
			Script.Parent.Part.Transparency = 1
			Script.Parent.Part.CanCollide = false
						
			end

as well if you have a better code feel free to share.

ok, revised issues.....

This is what I have now it works kind of....

It will change one block transparency and not the others.

help please... lol

this is how it is set up

Workspace

 Model

      Script

      Model

      Model

      Model

      Model

      Model

      Model

      Model

This is the script

for i,v in pairs(script.Parent:GetChildren()) do 
    if not v:IsA("Script") then 
        v.Humanoid.Changed:connect(function() 
            if script.Parent.Model.Humanoid.Health > 0 then 
                script.Parent.Model.Part.Transparency = 0 
                script.Parent.Model.Part.CanCollide = true 
            elseif script.Parent.Model.Humanoid.Health <= 0 then 
           script.Parent.Model.Part.Transparency = 1 
                script.Parent.Model.Part.CanCollide = false 
            end 
        end) 
    end 
end 

1 comment

0

Fixed mine

1 answer

✓ Accepted answer

You had a few syntax errors..

script.Parent.Humanoid.Changed:connect(function()
	if script.Parent.Humanoid.Health > 0 then
		script.Parent.Part.Transparency = 0
		script.Parent.Part.CanCollide = true
	elseif script.Parent.Humanoid.Health <= 0 then
		script.Parent.Part.Transparency = 1
		script.Parent.Part.CanCollide = false
	end
end)

Oh! You never said you wanted all the blocks to become invisible! Here's the fixed one!

for i,v in pairs(script.Parent:GetChildren()) do
	if not v:IsA("Script") then
		v.Humanoid.Changed:connect(function()
			if v.Humanoid.Health > 0 then
				v.Part.Transparency = 0
				v.Part.CanCollide = true
			elseif v.Humanoid.Health <= 0 then
				v.Part.Transparency = 1
				v.Part.CanCollide = false
			end	
		end)
	end
end)

14 comments

0

Thanks for the answer, but i put this in my script and it still isnt changeing the Transparency of my brick. ( I have a model and in the model i have a humanoid, and a part and the script. The part is what i am trying to make go away after someone kills the block.)

0

@snipers0076 Try now.

0

thanks alot, it worked, i didnt see any change. what did you change in the code?

0

i cant raise your score or i would, i need 4 more points to beable to.. but thanks alot for the help. You will see lots of me on here, I am new to LUA and trying to learn lol.

0

Hello, Thank you for your help. I was just wondering if it wouldn't be better if I put the script in a model and the 100 or so blocks that I need to have hit points on in the same model. instead of putting the script in each block. have one script for all of the blocks. what do you thank would that allow my game to have less lag?

0

@snipers0076 For my changed event, I just removed the ".Health" part. If you want to have this script work for multiple parts, within one script, I recommend using the generic loop. I'll do that now

0

thanks for the reply again, but this one dont seam to work. i fixed the typo in children, and the extra ) but it still dont work. i have my model and in that model i placed the script, in the same model is the other blocks that the script will affect. I have been messing with this script all day lol...

0

this is what it gives me...(10:37:01.833 - Humanoid is not a valid member of Model) im working on it, if i get it i wil repost

0

wow, i dont know why this scrips is giving me such a problem. all i want is a wall for my base that can be attacked and when it health hits 0 it goes away... lol man all day on 1 script

0

Hm. That depends with your error. Where is the Humanoid located? Please give the Hierachy (Full location)

0

yea i tried but this web page messed up my format......

0

i put it up top, i hope you can understand it.... it is workspace, and in there i have a model with 10 models in it, as well as the script.. the 10 models each have a humanoid inside and a part (block) the humanoid gives the part health so it can be destroyed..

0

Nice!, thanks alot it works ..... thanks for all of your help

0

ok, i am not sure if you got my messages but, i am having problems with lag. Will to many blocks lag the game out?

This discussion is preserved as it was. New answers, comments, and votes are closed.