I am new at ROBLOX scripting and have made the following code/script to make an object spawn infinitely.
while true do
wait(2)
hello = Instance.new("Part")
hello.Position = Vector3.new(0,22,0)
hello.Anchored = false
hello.CanCollide = false
hello.BrickColor = BrickColor.new("Baby blue")
hello.Parent = game.Workspace
end
However, I want to make it so that when the person hits the object they take damage and eventually die. I have tried looking up touch events that make this happen but do not seem to work.
1 | "use code blocks" |
Anyways, I'm also quite new to coding. I'm certainly no expert (or even good) at scripting but here's how you fix it I think:
1 | while true do |
2 | local newPart = Instance.new( "Part" , game.Workspace) |
3 | newPart.Name = "Part" |
4 | newPart.Anchored = false |
5 | newPart.Position = CFrame.new( 0 , 60 , 0 ) |
6 | newPart.BrickColor = BrickColor.new( "Baby blue" ) |
7 | wait( 2 ) |
8 | end |
tl;dr: Put the wait at the end
I've optimized your scripted
01 | while true do |
02 | wait( 2 ) |
03 | hello = Instance.new( "Part" ) |
04 | hello.Position = Vector 3. new( 0 , 22 , 0 ) |
05 | hello.Anchored = false |
06 | hello.CanCollide = false |
07 | hello.BrickColor = BrickColor.new( "Baby blue" ) |
08 | hello.Parent = game.Workspace |
09 | script.Parent.Touched:Connect( function (hit) |
10 | if hit.Parent:FindFirstChild( "Humanoid" ) then -- if whatever hits has a humanoid then fire an event |
11 | hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 25 -- The amount of damage change this to your preference. |
12 |
13 | end |