Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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

0
also, `:Connect` with a lower "c" is deprecated, use `:Connect` with an uppercase "c" or your scripts will soon all be broken. cmgtotalyawesome 1418 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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
0
He actually does need debounce or his game will crash. Too many explosions cause a lot of lag. cmgtotalyawesome 1418 — 6y
0
oh okay greatneil80 2647 — 6y

Answer this question