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

How do I remove a spawned part when the local player has died/disconnected?

Asked by 8 years ago
Edited 8 years ago

-- Sorry if this has been posted before; I looked up local scripts and removing parts on death but I couldn't find anything specifically related to my question.

How is this done?

Details:

1) When a player spawns, they spawn with a small floating red orb above them - This is created by a local script in the Player's StarterPack. 2) That floating orb is known as Player.Name.."'s_BasicOrb"

How can I remove that orb when the local player either dies or disconnects?

Here is the code I've gotten to work so far:

local Player = game.Players.LocalPlayer

Player.CharacterAdded:connect(function(PlayerBody)  

    BasicOrb = Instance.new("Part", game.Workspace)
    BasicOrb.Name = Player.Name.."'s_BasicOrb"
    BasicOrb.Size = Vector3.new(2,2,2)
    BasicOrb.Anchored = true
    BasicOrb.Shape = "Ball"
    BasicOrb.Material = "Neon"
    BasicOrb.BrickColor = BrickColor.new("Really red")

    while wait() do
        BasicOrb.CFrame = PlayerBody.Head.CFrame:toWorldSpace(CFrame.new(0,10,0))
    end
end)

Issues:

1) Orb does not despawn on death. This is because I can't seem to make a function that actually removes the orb. 2) Script gets stuck in the while loop, so anything else written in the script is ignored. 3) Orb is basically duplicated because - When the player dies, the first orb is not removed - When the player respawns, another orb is created - I want the orb to be recreated each time the player respawns (that way, if the orb is deleted somehow, the player can reset and the orb will pow exist again.)

Any valid input would be much appreciated by me, as I have been struggling with this issue for a few days now. #NewbieIssues.

PS: I can't just say

Player.Died:connect(function() BasicOrb:remove()) 

can I? I mean... I don't have any playtesters, but how would the game know to remove the part that it spawn for the local player and not a different player's part?

0
PPS: I've been doing some testing of the current local script and I've noticed that the floating orb flickers. It didn't do that when I had the script as a simple game.Players.PlayerAdded:connect(OrbFollow) function, so I'm not sure why it'd change when it's a local script. If you'd like to answer this question, too, that'd be very helpful :3 alikabeth 25 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Clarification,

Functions and events can be hidden, and called when the script deems ready. This means we basically CAN do what you suggested. As K3du53 said, "the Died event is called on the humanoid".

Your main questions seems to be, "Where do I put the code?". This is rather simple. Because nothing will run after the first while loop, we should place the anonymous function above the while loop.

local Player = game.Players.LocalPlayer

Player.CharacterAdded:connect(function(PlayerBody)  

    BasicOrb = Instance.new("Part", game.Workspace)
    BasicOrb.Name = Player.Name.."'s_BasicOrb"
    BasicOrb.Size = Vector3.new(2,2,2)
    BasicOrb.Anchored = true
    BasicOrb.Shape = "Ball"
    BasicOrb.Material = "Neon"
    BasicOrb.BrickColor = BrickColor.new("Really red")

    PlayerBody:WaitForChild("Humanoid").Died:connect(function()
        BasicOrb:Destroy()
    end)

    while wait() do
        BasicOrb.CFrame = PlayerBody.Head.CFrame:toWorldSpace(CFrame.new(0,10,0))
    end
end)
Never use the Remove Function. This function has been deprecated. Use Destroy.

Precautions,

When the character dies, the Orb gets Destroyed. This will error in the while loop when the script tries to set the CFrame of a nil value. To fix this, I would check in the while loop if the Orb exists before setting it's CFrame will a basic conditional statement.

local Player = game.Players.LocalPlayer

Player.CharacterAdded:connect(function(PlayerBody)  

    BasicOrb = Instance.new("Part", game.Workspace)
    BasicOrb.Name = Player.Name.."'s_BasicOrb"
    BasicOrb.Size = Vector3.new(2,2,2)
    BasicOrb.Anchored = true
    BasicOrb.Shape = "Ball"
    BasicOrb.Material = "Neon"
    BasicOrb.BrickColor = BrickColor.new("Really red")

    PlayerBody:WaitForChild("Humanoid").Died:connect(function()
        BasicOrb:Destroy()
    end)

    while wait() do
        if BasicOrb then
            BasicOrb.CFrame = PlayerBody.Head.CFrame:toWorldSpace(CFrame.new(0,10,0))   
        else
            break;
        end
    end
end)
Break will "break out" of a loop.

Possible Future Errors

I understand you're using Local Player, and this should mean you're using a LocalScript. You should put this Local Script in ReplicatedFirst, StarterPack, StarterPlayerScripts, or some other replicated service that Local Scripts can be run in. However, depending on if you have FE, "Filtering Enabled", Enabled, you might want to make this system Server-Sided so other people can see the orbs. I wouldn't discourage using FE on a new place, but converting an old place to FE will almost certainly cause you problems. In the future however, make sure you understand when it's better to have things Client Sided Vs. Server Sided.

Good Luck!

It would be awesome if you accepted an answer. This answer doesn't have to be mine, but the one that helped you the most or solved your problem the best. Thank you.
0
Okay! I will try this. As I said in my question, though, I have the local script located in the StarterPack :P so no need to worry about server side/client side with that cx. EDIT: I didn't know that 'ENTER' saved a comment. Anyway, I don't know what 'waitForChild()' does/I've never used it but I assume that it's like an event that waits for one of the object's children to have an event done to th alikabeth 25 — 8y
0
It waits for things to load. User#11440 120 — 8y
0
To make the orb smoother, I would suggest using body movers. That's just me. User#11440 120 — 8y
0
Woohoo! It works :3 Also, what do you mean by 'it waits for things to load'? Does that mean that it waits to make sure that the humanoid character actually exists in game and then checks to see if it has died? Is this something that I could just look up or will it confuse me ;-;? PS: What are 'body movers'? Because I've been wondering about how to make things move smoothly but I cannot find any g alikabeth 25 — 8y
Ad
Log in to vote
0
Answered by
k3du53 162
8 years ago
Edited 8 years ago

Noting, if you were to turn FE on, the orb would only show for the player who owns the orb.

The function,

Player.Died:connect(function()
    BasicOrb:Remove()
end)

should work because BasicOrb is referenced earlier in the script. (but with some modifications)

First, the Died event is called on the humanoid, so that would be replaced with:

Player.Character.Humanoid.Died:connect(function()
    BasicOrb:Remove()
end)

Next, so that the orb doesn't stay hidden in nil (I'm not sure how the garbage collector works), we destroy reference to the orb, or call destroy on it.

Player.Character.Humanoid.Died:connect(function()
    BasicOrb:Remove()
    BasicOrb = nil
end)

OR

Player.Character.Humanoid.Died:connect(function()
    BasicOrb:Destroy()
end)

(I'm really bad at closing sentences. Good luck?!)

0
But where can I put that to run it? I'm, after all, using a pesky 'while' loop in there, and that is acting as an eddy from which the script, um, parser (?) will never escape. If I put it into the while loop, the while loop will stop telling the orb to hover above the character's head. alikabeth 25 — 8y
1
I added some clarification and a few extras for our friend here. Good answer. User#11440 120 — 8y

Answer this question