Roblox- I am incredibly inexperienced at this and have not enough information to figure out what I have done wrong.
boom = Instance.new("Part", workspace) function touch(oops) if workspace:findFirstChild("boom") then boom.Position = Vector3.new(234, 190.5, 96) boom.Shape = 0 boom.BrickColor = BrickColor.new("Bright Yellow") boom.Transparency = 0 boom.Anchored = true boom.CanCollide = false for i = 1, 100 do boom.Size = Vector3.new(i, i, i) wait(.5) end end end script.Parent.Touched:connect(touch) function touch(hit) wait(.5) hit.Parent:findFirstChild("Head"):Remove( end game.Workspace.boom.Touched:connect(touch)
The goal was an 'explosion'- a ball appearing and growing across the screen until a certain size, killing all it touched. I was thinking maybe it was the hit.Parent:findFIrstChild part, but even if that were the case I wouldn't know how to reprimand it.
Starting clean
Before we go any further, it always helps to clean up our code a bit before trying to debug it. Sometimes you'll even find yourself fixing the bug as you clean it. This can be done simply with the proper use of line indentation, local variables when needed, and organization. I'll give an example of how this can be done as I walk through the problems.
Problems
1: The Remove function
The Remove
function on line 19
of your script not being called correctly. In fact, it's a syntax error
. An honest mistake or typo everyone goes through at some point, but your entire program won't even run if something is syntactically incorrect.
2: Functions
You have 2 functions given the same variable name: 'touch'. Lua reads code from top to bottom, left to right, and creating a function with the exact same name as one above it, will overwrite it with that new value. Making the one above it completely useless.
3: Variables
Proper use of your variables. This isn't technically an error, but makes your program extremely prone to causing one if you're not careful. The 'boom
' variable you created can be referenced instead of indexing the workspace
or game model
to find the object you created.
4: Object names / Variables
Variables / Object names are not the same. Creating a variable called "boom" will not name your newly created object that as well. The default name for a newly created part, is "Part". To name it something else, you can access it's "Name" property and set it a new value. However, this won't be necessary in this case given the explanation above about utilizing variables.
Final result
Here would be your revised code:
-- I just feel more comfortable working with locals local boom = Instance.new("Part", workspace) local activated = false -- Changed "oops" to "part", just to make it easier to understand local function touch(part) -- This is called a debounce method. You should look more into them, they're very useful. if activated then return end activated = true -- Probably best to use CFrame in this case boom.CFrame= CFrame.new(234, 190.5, 96) boom.Shape = 0 boom.BrickColor = BrickColor.new("Bright Yellow") boom.Transparency = 0 boom.Anchored = true boom.CanCollide = false -- Again we'll use CFrame, so the part won't react with physical disruptions around it. CFrame is relevant in this case to keep the ball in it's origin position, so it won't be effected by the Vector change. for i = 1, 100 do boom.Size = Vector3.new(i,i,i) boom.CFrame = CFrame.new(234, 190.5, 96) wait(0.5) end activated = false end -- boom is a variable that represents the part it created. So we may reference it by just indexing the script for "boom", like so: boom.Touched:connect(touch)
Hope it helped. Let me know if you have any questions.
So your issue here is that you never called your function, so all it did was put the part in workspace and fired it when touched. It's a rather simple and honest mistake. You also never named the part. This means that you can not use >:FindFirstChild() because it won't find an object to match your string. I would recommend using child added since it seems more reliable in my opinion. Here is our improved script
game.Workspace.ChildAdded:connect(function(object) print(object.Name) if object.Name == "Boom" then object.Position = Vector3.new(234, 190.5, 96) object.BrickColor = BrickColor.new("Bright Yellow") object.Transparency = 0 object.Anchored = true object.CanCollide = false for i = 1, 100,1 do object.Size = object.Size + Vector3.new(1,1,1) wait() print(i) end end end) boom = Instance.new("Part") boom.Name = "Boom" boom.Parent = game.Workspace print("Script runs") boom.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then hit.Parent:FindFirstChild("Head"):Destroy() wait() end end)
I would also recommend not using >Remove() . A better option would be >Destroy() . Remove() doesn't actually remove the object for it can be re-parented, while destroy() removes the parts complete existence