-- 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?
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)
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)
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!
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?!)