I'm trying to change a GUI when a player dies, so I have a server script detect that. Then it send the event to a local script that's supposed to then change the GUI (I want the "lives" on the GUI to decrease when the player dies). But I'm getting errors, and only the server script prints anything. I have the remote event in repstorage, and my local script is in the GUI. My server script is in serverscriptstorage or whatever.
here the code from server script
01 | local player = game:GetService( 'Players' ).PlayerAdded:Connect( function (player) |
02 | local char = player.CharacterAdded:Connect( function (character) |
03 | local hum = character:WaitForChild( "Humanoid" ) |
04 | local repstorage = game:GetService( "ReplicatedStorage" ) |
05 | local DeathEvent = repstorage:WaitForChild( "DeathEvent" ) |
06 |
07 |
08 |
09 | hum.Died:connect( function () |
10 | print ( "player has died! detected from server" ) |
11 | DeathEvent:FireClient(player) |
12 |
13 | end ) |
14 | end ) |
15 |
16 | end ) |
this is the code from my LOCAL script
01 | local player = game:GetService( 'Players' ).PlayerAdded:Connect( function (player) |
02 | local char = player.CharacterAdded:Connect( function (character) |
03 | local hum = character:WaitForChild( "Humanoid" ) |
04 |
05 | local DeathEvent = game.ReplicatedStorage.DeathEvent |
06 | local livestext = "lives: " |
07 | local numberoflives = 3 |
08 | local lives = livestext .. numberoflives |
09 | local text = script.Parent.Text |
10 |
11 |
12 | local function deathfunction() |
13 | print ( "player has died! remote event worked" ) |
14 |
15 | text = "is it working" |
Please explain your answers, I don't just want to get this to work, I want to learn and understand everything.
On your first script, everything is fine except for line 11. Instead of ":FireClient()", you are supposed to use ":FireAllClients()". Though this fires all clients instead of just one, you can always check whether you are the player who died using the local script since you are sending back the "player" variable through the ":FireAllCients()".
Here is your edited script:
01 | local player = game:GetService( 'Players' ).PlayerAdded:Connect( function (player) |
02 | local char = player.CharacterAdded:Connect( function (character) |
03 | local hum = character:WaitForChild( "Humanoid" ) |
04 | local repstorage = game:GetService( "ReplicatedStorage" ) |
05 | local DeathEvent = repstorage:WaitForChild( "DeathEvent" ) |
06 |
07 | hum.Died:connect( function () |
08 | print ( "player has died! detected from server" ) |
09 | DeathEvent:FireAllClients(player) |
10 | end ) |
11 | end ) |
12 | end ) |
On your local script, you can just use
1 | local player = game.Players.LocalPlayer |
instead, as it is in a local script. Otherwise, it should work.
Here is your edited local script:
01 | local localplayer = game:GetService( 'Players' ).LocalPlayer |
02 | local char = localplayer.Character |
03 | local hum = char:WaitForChild( "Humanoid" ) |
04 | local DeathEvent = game.ReplicatedStorage.DeathEvent |
05 | local livestext = "lives: " |
06 | local numberoflives = 3 |
07 | local lives = livestext .. numberoflives |
08 | local text = script.Parent.Text |
09 |
10 | DeathEvent.OnClientEvent:Connect( function (player) --condenses the creation of the function and connects it to the event in the same line |
11 | if player = = localplayer then --Check if the player mentioned in the event is the same as the gui owner... |
12 | print ( "player has died! remote event worked" ) |
13 | text = "is it working" |
14 | end |
15 | end ) |
Tell me if anything breaks.
@demcode, yes, your version partially worked. to get the text to change upon death I made it like this (local script, the server script was fine)
01 | local localplayer = game:GetService( 'Players' ).LocalPlayer |
02 | local char = localplayer.Character |
03 | local hum = char:WaitForChild( "Humanoid" ) |
04 | local DeathEvent = game.ReplicatedStorage.DeathEvent |
05 | local livestext = "lives: " |
06 | local NumberOfLives = 3 |
07 | local livesGui = script.Parent |
08 |
09 | DeathEvent.OnClientEvent:Connect( function (player) --condenses the creation of the function and connects it to the event in the same line |
10 | if player = = localplayer and NumberOfLives > 0 then --Check if the player mentioned in the event is the same as the gui owner... |
11 |
12 | livesGui.Text = livestext .. tostring (NumberOfLives - 1 ) |
13 | NumberOfLives = NumberOfLives - 1 |
14 |
15 | elseif player = = localplayer and NumberOfLives < = 0 then |
16 | livesGui.Text = "you have no more lives" |
17 | end |
18 | end ) |
in my game once the player has no more lives I will teleport them.