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

How can I fix my script that is supposed to explode the player?

wait(5)
local function explode()
	local player = game.Players.LocalPlayer
	repeat wait(0.1) until player.Character
	local handle = player.Character:WaitForChild("Torso")
	
	local explosion = Instance.new("Explosion")
	explosion.Position = handle.Position
	print("Exploded")
end

explode()

I know the cause of issue, it counts character nil because it hasnt loading in yet, but I dont know how to fix it

1 answer

✓ Accepted answer

Problem:

There's no need of the wait on line 1, since the player would already have been in the game. I can't see any really see any errors within your code but try this code

##Fixed Code:

local function explode()
	local player = game.Players.LocalPlayer 
	repeat wait() until player.Character
	local handle = player.Character:FindFirstChild("HumanoidRootPart")

	local explosion = Instance.new("Explosion")
	explosion.Position = handle.Position
	print("Exploded")
end

explode()

Ensure this is a LocalScript placed in StarterPlayer < StarterPlayerScripts

##Recommendation(s)

  • Use HumanoidRootPart when working with events like this, because let's say an R15 rig joins the game it wouldn't work because R15 don't have a torso
  • FindFirstChild is better (in this case) because WaitForChild yeilds until the thread exits whilst FindFirstChild finds the child of the given instance immediately unlike WaitForChild.

##JesseSong

1 comment

0

remember to parent the explosion or else it won't work. Here's a guide on explosions: https://www.youtube.com/watch?v=Rj-_cjGfCCY

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