Script works weirdly

hey there, basically the script only works after the player respawns, not dies. (server script)

	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			--print(player.Name .. " has died!")
			game.StarterGui.PlayerDeath.TextLabel.Visible = true
			game.StarterGui.PlayerDeath.TextLabel.Text = player.Name .. " has died!"
			game.StarterGui.PlayerDeath.TextLabel.Boom.Playing = true
		end)
	end)
end)

also, the sound doesnt play ( even with :Play() )

This seems like an unusual method, have you tried using RemoteEvents? It looks like it’d help you in this scenario, if you’re wanting to replicate the Gui to all clients

2 Likes

When the game starts, everything in the StarterGui is cloned in player.PlayerGui.
So in your script changing game.StarterGui to player.PlayerGui should fix the issue.

Since everyone above me is saying the StarterGui clones the Ui into the PlayerGui which is inside the player (this is true by the way), you’ll need to get the Ui inside the player while the player is being added. Getting it from StarterGui works, but it shows it to everyone in the game once it is executed I think.

game.Players.PlayerAdded:Connect(function(plr)
plr.PlayerGui:WaitForChild(“TheGui”).Visible = true
end)

something like that

That won’t be replicated across every client though if it’s a PlayerDeath screen & the text is changing for that certain player to die, which wouldn’t make sense

Again, RemoteEvents would be something to look into: They can be used from server-client transportation

--Your script
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")
			Event:FireAllClients(character.Name)
		end)
	end)
end)
--LocalScript inside your Gui
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local DeathText = PlayerGui:WaitForChild("PlayerDeath").TextLabel
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnClientEvent:Connect(function(VictimName)
    DeathText.Visible = true
    DeathText.Text = VictimName.." has died!"
    DeathText.Boom.Playing = true
end)
1 Like

Correct me if I’m wrong, but this wouldn’t be very handy server sided. It is most likely he needs to use Remote Events/Functions for Server-Client communication.

Also (don’t quote me on this, I’ve not worked with UI for quite a while.)

Changing it on the StarterGui will not change on the client. Every time a player joins the StarterGui is replicated to the client. Meaning the client does not fetch new changes that happen in StarterGui. Think of StarterGui as a starting template; but the template always has to be changed on the client.

1 Like

Probably in this case. I am not too experienced with using events/functions since they hurt my head trying to understand the client side of them, but yes.

Think of a RemoteEvent as basically a train transporting goods

It’s transferring the goods from Location A to Location B

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Goods = {"Oranges", "Apples", "Bananas", "Tomatoes"}

Event:FireServer(Goods)

We first fire the Event from Location A, so that it’s being transported from the client to the server (Or Location B)

And on our other script:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(Player, Goods)
    print(Goods)
end)

We’re receiving the goods that was first brought to us from Location A

1 Like

This is easier:

local Player = game:GetService("Players").LocalPlayer
PlayerGui = Player:WaitForChild("PlayerGui"):WaitForChild("PlayerDeath")

script.Parent:WaitForChild("Humanoid").Died:Connect(function()
	print(Player.Name .. " has died!")
	PlayerDeath.TextLabel.Visible = true
	PlayerDeath.TextLabel.Text = Player.Name .. " has died!"
	PlayerDeath.TextLabel.Boom.Playing = true
end)

(Use localscript and put it in StarterCharacterScripts.)