Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
ym5a 52
3 years ago
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

Log in to vote
1
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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

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 JesseSong 3916 — 3y
Ad

Answer this question