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

Can you make a script to explode yourself to damage others?

I'm trying to make a script where a player can press a button (in this case, the Q key) and explode, damaging other people. Basically, it's a script to self-destruct yourself. I am quite new to scripting so I'm only using basic functions like Instance.new but I can learn more advanced terms if I need to. I got different parts of the script from this website on various questions. Here is the script so far:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "q" then
		Instance.new("Explosion", player)
	end
end)

This script doesn't work at the moment and I was wondering if someone could help me. Thanks in advance, and sorry if this is a stupid question! P.S. This is in a LocalScript.

4 comments

0

further define the explosion

0

How, exactly?

0

This script is very outdated

0

Do you think you could clarify how to write it?

1 answer

A few things wrong with your script.

  1. Don't use mouse:KeyDown, use UserInputService.InputBegan instead. UIS is more reliable and up to date. https://developer.roblox.com/api-reference/event/UserInputService/InputBegan

  2. The second argument of Instance.new() is deprecated, which means you shouldn't use it.

  3. You have to make the explosion a variable. (local explosion = Instance.new("Explosion")

Now that we got the problems out the way, let's go over and see how to fix the script: You need a character defined (in a variable). A player is the client, where a character is the person you walk around as. You also need to add the explosion variable, so you can manage it easier. So, your script should start like this:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait() --- Get the character.
local explosion = Instance.new("Explosion")

That's better. Now, you started off the explosion right. Explosions, well, explode as soon as they're parented into workspace, or a descendant of workspace. So, all you have to do is fix this simple script, using the help I gave you. Keep in mind that you should :Clone() the explosion and make a 2nd variable for it, then parent it to the character's humanoidrootpart, so the first explosion doesn't dissapear from the game's memory.

Keep in mind this is local sided, so it will explode only you, other players won't see the explosion or be damaged by it. To make it work with other people, learn about remotes and experiment around instead. Full script for future refference below:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait() --- Get the character.
local explosion = Instance.new("Explosion")
local UIS = game:GetService("UserInputService") --- Get the UserInputService.

UIS.InputBegan:Connect(function(input,gp)
	if not gp and input.KeyCode == Enum.KeyCode.Q then
		local ex2 = explosion:Clone()
		ex2.Parent = char.HumanoidRootPart
	end
end)

If you found this useful, mark this as the answer!

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