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

How do I fix my script for a click-button bomb spawner?

I've been working on my game lately Retro Wars which involves different weapons, vehicles, and buildings for warfare and I have just now started working on my plane that drops bombs. I would like to create the script myself for which drops bombs instead of trying to find a free-modeled version of what I'm trying to find. I'm a beginner at scripting and have been teaching myself for a while, so I know some things but not everything.

Problem: The script that I have made does not work and I don't know what I'm doing wrong in the script to make it not work. How the bomb spawner works is there is a click button, the parent of this script which also has the clickdetector in it. The parent of the script is called "bomber" and is inside a model with another part called "bombdoor". When the button is clicked with the mouse, a bomb is supposed to spawn and position itself automatically at the bombdoor then be dropped. When it hits an object, it is supposed to explode on contact.

function onClicked(bomber)
	local bomb = Instance.new("Part")
	local bombdoor = script.Parent.Parent.bombdoor
	bomb.Position = bombdoor
	bomb.Anchored = false
	bomb.CanCollide = true
	bomb.Locked = true
	bomb.Size = 2,2,2
	bomb.Shape = 0
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)


function onTouched(hit, bomb)
	if bomb == nil then return end
	local blowup = Instance.new("Explosion")
	local bombsound = Instance.new("Sound")
	bombsound.SoundId = "rbxassetid://357562152"
	blowup.BlastPressure = 300
	blowup.BlastRadius = 40
	blowup.Position = bomb.Position
	blowup.Parent = game.Workspace
	if bombsound then bombsound:Play()
		bombsound.Position = bomb.Position
	end
	wait(.1)
	if bomb then bomb:Remove() end
end

--missing the part that comes at the end of the function like line 12

1 comment

0

You create the bomb but do not add it to the workspace, e.g. bomb.Parent = game.Workspace, you can also add the parent to the instance.new as it is an optional paramater. You also need to add the touched event for the bomb.

1 answer

For this kind of thing it might be best to create one bomb then create clones of the one bomb created. The other issues are that you do not connect a Touched event to the bomb, sound does not have a position simply put the sound in a part then it will play from that part and you should not be using remove you should be using Destroy()

Explanation:-

function onClicked(bomber)
    local bomb = Instance.new("Part") -- we have not parented the bomb in this code block so it is not added to the game 
    local bombdoor = script.Parent.Parent.bombdoor -- bombdoor  is a part we need its Position  and may also need an offset
	-- it would be better to use a CFrame
    bomb.Position = bombdoor
    bomb.Anchored = false
    bomb.CanCollide = true
    bomb.Locked = true
    bomb.Size = 2,2,2
    bomb.Shape = 0
	-- we have not connected a touched event for the bomb no other code will run 
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)


function onTouched(hit, bomb)
    if bomb == nil then return end
    local blowup = Instance.new("Explosion")
    local bombsound = Instance.new("Sound") -- if you do not parent the sound to a part it will not be 3D
    bombsound.SoundId = "rbxassetid://357562152"
    blowup.BlastPressure = 300
    blowup.BlastRadius = 40
    blowup.Position = bomb.Position
    blowup.Parent = game.Workspace
    if bombsound then bombsound:Play()
        bombsound.Position = bomb.Position -- sound does not have a Position
    end
    wait(.1)
    if bomb then bomb:Remove() end -- we should be using  Destroy
end

--missing the part that comes at the end of the function like line 12

My example, you should only use this as a reference:-

-- create the bomb 
local bomb = Instance.new('Part')
bomb.Name = 'Bomb'
bomb.Anchored = false
bomb.CanCollide = true
bomb.Locked = true
bomb.Size = 2,2,2
bomb.Shape = Enum.PartType.Ball -- it is good to use Enums

-- For 3D sound the sound must be places inside a part, the sound will then play from the part
local bombsound = Instance.new('Sound', bomb) -- this is parented to the bomb
bombsound.Name = 'ExplosionSound'
bombsound.SoundId = 'rbxassetid://357562152'

-- create the explosion
local blowup = Instance.new('Explosion')
blowup.BlastPressure = 300
blowup.BlastRadius = 40

-- game vars
local bombDoor = script.Parent.Parent.bombdoor

local function bombHit(hitPart, tmpBomb)
	tmpBomb.Anchored = true  -- may want to anchore the bomb
	local expClone = blowup:Clone() -- clone the pre made explosion
	expClone.Position = tmpBomb.Position -- moves the explosion to the point of the bomb
	expClone.Parent = game.Workspace
	tmpBomb.ExplosionSound:Play() -- there are other methods such as, play on remove  which plays the sound when it is deleted
	tmpBomb.ExplosionSound.Ended:Wait() -- holds the thread till the explosion sound has ended
	tmpBomb:Destroy() -- we sould not be using remove
end

local function onClicked(clickedPlayer) -- the param passed is the player who clicked the click detector
	-- we clone from our pre made bomb
	local tmpBomb = bomb:Clone() -- clone the bomb 
	local deb = false -- the deboucne
	tmpBomb.Position = bombDoor.Position -- you might be better off using the CFrame, you may also need an offset to move the bomb down so its no inside the bomb door
	tmpBomb.Parent = game.Workspace -- we add the bomb to the game
	tmpBomb.Touched:Connect(function(hit) 
		if deb then return end -- stops multiple requests 
		deb = true -- this will now only run once
		bombHit(hit, tmpBomb) -- we pass the hit  then tmpBomb
	end) 
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I have tried to commend the above code as much as possible to explain how it works, this is only my example and this may not be the best solution for you but it explain the parts needed for the creation / setup of the bomb.

I hope this helps, please comment if you need any more info.

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