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

How do I make glass shatter when it's shot 10 times?

Hello, how would I make some glass shatter if it's shot 10 - 20 times?

2 answers

✓ Accepted answer

I am going to answer, "How to make glass shatter when it is shot 10 times?".

Well, what you need to do is to 'characterize' glass. You have to put a value into it (a number value) that starts at 0. Next, insert a script that would check every time this number changes if it has reached the number 10. In this case, the value is in the glass part, same with the script. Also, make sure the part's name is "Glass". So, what happens in the code below is that if the value number changes, then it checks if it has reached the number 10 or higher (in case of lag). If the number is 10 or higher, then the glass will be destroyed, along with the number value and your script. However, the glass will still be there, with the script, when you stop 'play mode' and enter into normal 'studio mode'.

script.Parent.NumberValue.Changed:Connect(function()
    if script.Parent.NumberValue.Value =< 10 then
	    script.Parent:Destroy()
    end
end)

Now we have our script that destroys the glass part when it's value is 10 or greater, we must implement a script into our bullets that increases this number by 1 every time the bullet touches the glass. In the code below, the script is inside the bullet. What happens is that every time the bullet touches anything, it will check if the part that was hit is called "Glass" and if it has a number value. If it does have the name "Glass" and has a number value, the script will add a number to the number value.

script.Parent.Touched:Connect(function(object)
    if object.Name == "Glass" and object:FindFirstChild("NumberValue") then
    object.NumberValue.Value = object.NumberValue.Value + 1
end)

That should be what you need. The first block of code detects if the number value is equal or greater than 10, then destroys if so. The second block of code checks if the bullet touches glass, and if so it adds a number to the glass. If you are wondering how to put the script into the bullet every time the bullet spawns, you can clone the script and set the cloned scripts parent into the bullet. Cloning tutorial is not here, but you could search through this site. I hope this answer helps you understand more of LUA.

1 comment

0

Do not answer this question you idiot. Read the guidelines. This question is not constructive.

You could put a Number Value and name it Health inside the glass. If it's been shot by a projectile, the projectile will take away some health from the value. If the health is equal to 0, glass will shatter.

3 comments

0

I posted my answer when there were no other answers, as it took several minutes to answer and make sure the code works.

0

:thumbs up: Thank you.

0

No problem.

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