Any alternative to this ui code?

I can see why this wouldn’t work… does anyone have any ideas on how to fix this?

local ui = game.ReplicatedStorage.ReplicatedUI

game.Players.PlayerAdded:Connect(function(plr)
	local uiobject = ui.StartUI:Clone()
	uiobject.Parent = plr.PlayerGui
	
	local character = plr.Character or plr.CharacterAdded:Wait()
	
	character.Humanoid.Died:Connect(function()
		local uiobject = ui.StartUI:Clone()
		uiobject.Parent = plr.PlayerGui
		
		character = plr.Character or plr.CharacterAdded:Wait()
	end)
end)

Could you please provide more information for this part here:

So all of the people helping here can work on this part here:


You are not giving what wouldn’t work because we don’t have access to the same context as you:

  1. Is ReplicatedUI not being found in ReplicatedStorage?
  2. Is this all in the wrong kind of script?
  3. Did you forget to use StarterGui?
  4. is there a local game = workspace somewhere in your script or something strange like that?
1 Like

Sorry. The issue here is that the ui only clones into the player twice (once when they load in, again when they first die). My suspicion is that the humanoid.Died event is connected to the original character, and not the updated reference that is created when the event fires. Do you know any way of how i might remedy this problem?

1 Like

@amazingmasterofluck if you are referring to the ui, Player ui is accessible both locally and server side, so this would not be an issue.

1 Like

Edit: I missed that you’re already using it, so I’m gonna reword the way I said that (sorry!)

Basically what I’d do is instead of using plr.CharacterAdded:Wait(), I’d wrap it into the event handler. Then it’ll keep running every time the player spawns and fix your problem.

2 Likes

oh yeah, ig that does make sense. i need to stop over complicating things

That worked, thank you

2 Likes

If you really like using wait you can do:

while true do
    local character = plr.Character or plr.CharacterAdded:Wait()

    -- set up the GUI
    
    character.Humanoid.Died:Wait()
end

Though that code is potentially problematic if you in some contrived way manage to assign an already dead character to the player.

3 Likes

I feel like a forever loop would consume a lot more resources on the server, and the event listeners are a better way to go. thanks for offering though

It not, it just executes every time the player dies, basically.

1 Like