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

Can someone help fix this script that makes part explode on touch?

So I tried making a script that made the part "Really red" when touched then it would explode, when I test it, it turns red but doesn't explode, I am new to scripting so expect some stupid mistakes.

debounce = true


script.Parent.Touched:connect(function(hit)
	humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid ~= nil and debounce == true then
		debounce = false
		player = game.Players:GetPlayerFromCharacter(hit.Parent)
		script.Parent.BrickColor = BrickColor.new("Really red")
        if part.BrickColor == BrickColor("Really red") then
	explosion = Instance.new("Explosion")
	explosion.Position = part.Position
	explosion.Parent = game.Workspace
	part.BrickColor = BrickColor.new("Institutional white")
	debounce = true
end
	end
end)

2 answers

You never defined "part", yet you used it. Instead, continue using script.Parent. That would make the script look like this:

debounce = true

script.Parent.Touched:connect(function(hit)
    humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and debounce == true then
        debounce = false
        player = game.Players:GetPlayerFromCharacter(hit.Parent)
        script.Parent.BrickColor = BrickColor.new("Really red")
        if script.Parent.BrickColor == BrickColor("Really red") then
    		explosion = Instance.new("Explosion")
   		explosion.Position = script.Parent.Position
    		explosion.Parent = game.Workspace
    		script.Parent.BrickColor = BrickColor.new("Institutional white")
    		debounce = true
		end
    	end
end)

or you could simply put another variable for part at the top to make it look like this (this would be the better solution):

debounce = true
part = script.Parent

part.Touched:connect(function(hit)
    humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and debounce == true then
        debounce = false
        player = game.Players:GetPlayerFromCharacter(hit.Parent)
        part.BrickColor = BrickColor.new("Really red")
        if script.Parent.BrickColor == BrickColor("Really red") then
    		explosion = Instance.new("Explosion")
   		explosion.Position = part.Position
    		explosion.Parent = game.Workspace
    		part.BrickColor = BrickColor.new("Institutional white")
    		debounce = true
		end
    	end
end)

The last thing that I noticed (which isn't an error, rather an unnecessary line of code), is that you defined the characters player, which you aren't using. If you aren't going to use this later on in the code, I suggest getting rid of it. Hope I helped.

-Cmgtotalyawesome

1 comment

0

also, :Connect with a lower "c" is deprecated, use :Connect with an uppercase "c" or your scripts will soon all be broken.

Script without debounce:

local part = script.Parent
part.Touched:connect(function()
	local Boom = Instance.new("Explosion", part)
---Add more properties for Boom---
end

In your script you said "Part" even though it doesn't exist in your script, the error you got was "Attempt to index a global "Part"" or something like that, make the script simple btw because you don't need debounce nor that other strange stuff you added in your code

script with debounce:

local part = script.Parent
local debounce = true
local waitTime = 2 -- Change to how long it takes the explode when touched again
part.Touched:connect(function()
if debounce == true then
	local Boom = Instance.new("Explosion", part)
	--Add more properties for Boom
	debounce = false
else
	wait(waitTime)
	debounce = true
end
end

2 comments

0

He actually does need debounce or his game will crash. Too many explosions cause a lot of lag.

0

oh okay

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