How to make a player explode when they say something

I have a script that tries to explode a player whenever they say a specific word but it doesn’t work. I think it’s a bug but I can’t figure out where it is.

Here is the code:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Explode" then
			print(""..player.Name.." Exploded") --this does print
			local explosion = Instance.new("Explosion")-- this doesn't work
			explosion.Position = player:FindFirstChild("HumanoidRootPart").Position-- this doesn't work
			explosion.Parent = player:FindFirstChild("HumanoidRootPart")-- this doesn't work
		end 
	end) 
end)

Please help!

1 Like

you should index the character. It should be player.Character:FindFirstChild(). You should also check to make sure the player’s character is alive:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Explode" then
			print(""..player.Name.." Exploded") --this does print
	        local character = player.Character
            if not character or not character:FindFirstChild("HumanoidRootPart")  then return end--Making sure the player is alive		
            local explosion = Instance.new("Explosion")
			explosion.Position = character.HumanoidRootPart.Position
			explosion.Parent = character.HumanoidRootPart
		end 
	end) 
end)
3 Likes

Do this in a server script:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Explode" then
			print(""..player.Name.." Exploded") --this does print
			local explosion = Instance.new("Explosion")-- this doesn't work
			explosion.Position = player.Character:FindFirstChild("HumanoidRootPart").Position-- this doesn't work
			explosion.Parent = player.Character:FindFirstChild("HumanoidRootPart")-- this doesn't work
		end 
	end) 
end)
1 Like

Thanks! though, the explosion never appears. Should I do

explosion.Visible = true

?

1 Like

If you do it in a server script it does appear like it did for me

1 Like

Wait was it in a local script to begin with?

No. It was in a server script in the workspace the whole time.

1 Like

Try saying “explode” in studio and check your character’s humanoid root part to see if its there

Oh. I see now. I have a Ragdoll script so it won’t appear because the Ragdoll script deletes the whole character when they die. How do I fix that?

1 Like

You can just parent it to workspace then delete it after a second or so. Using debris service would be useful

Parent what to workspace?

Summary

This text will be hidden

1 Like

the explosion instance

1 Like

His player is being deleted by his ragdoll script before the player can see the explosion(I think).

OP, change your ragdoll script, or just disable it while you test the explosion thing.

I see what you’re saying, if its not parented to the character then problem solved(maybe)

1 Like

Somethings wrong… the script fires every time a player chats

2 Likes

Can I see your current script?

1 Like

Here:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Explode" then
			print(""..player.Name.." Exploded") --this does print
	        local character = player.Character
            if not character or not character:FindFirstChild("HumanoidRootPart")  then return end--Making sure the player is alive		
            local explosion = Instance.new("Explosion")
			explosion.Position = character.HumanoidRootPart.Position
			explosion.Parent = workspace
		end 
	end) 
end)

If you want to do this one time do:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "Explode" then
			print(""..player.Name.." Exploded") --this does print
	        local character = player.Character
            if not character or not character:FindFirstChild("HumanoidRootPart")  then return end--Making sure the player is alive		
            local explosion = Instance.new("Explosion")
			explosion.Position = character.HumanoidRootPart.Position
			explosion.Parent = character.HumanoidRootPart
            script:Destroy()
		end 
	end) 
end)

game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if msg == “Explode” or “explode” then
print(""…player.Name…" exploded")
local character = player.Character
if not character or not character:FindFirstChild(“HumanoidRootPart”) then return end–Making sure the player is alive
local explosion = Instance.new(“Explosion”)
explosion.Position = character.HumanoidRootPart.Position
explosion.Parent = workspace
end
end)
end)

I see why thats happening, it’s this line:

if msg == “Explode” or “explode” then

The if statement will always amount to true because checking a string by itself will always amount to true. You could try checking it against the “msg” variable too or you could just use string.lower(). Something like this:

if msg == "Explode" or msg == "explode" then

OR:

if msg:lower() == "explode" then
1 Like
game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg:lower() == "explode" then
			print(player.Name.." Exploded")
			local explosion = Instance.new("Explosion")
			explosion.Parent = player.Character.PrimaryPart
			explosion.Position = player.Character.PrimaryPart.Position
		end 
	end) 
end)

this personally works for me (Edited)