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)
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)
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)
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)
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: