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
local player = game:GetService('Players').PlayerAdded:Connect(function(player) local char = player.CharacterAdded:Connect(function(character) local hum = character:WaitForChild("Humanoid") local repstorage = game:GetService("ReplicatedStorage") local DeathEvent = repstorage:WaitForChild("DeathEvent") hum.Died:connect(function() print("player has died! detected from server") DeathEvent:FireClient(player) end) end) end)
this is the code from my LOCAL script
local player = game:GetService('Players').PlayerAdded:Connect(function(player) local char = player.CharacterAdded:Connect(function(character) local hum = character:WaitForChild("Humanoid") local DeathEvent = game.ReplicatedStorage.DeathEvent local livestext = "lives: " local numberoflives = 3 local lives = livestext .. numberoflives local text = script.Parent.Text local function deathfunction() print("player has died! remote event worked") text = "is it working" end end) end) DeathEvent.OnClientEvent:Connect(deathfunction)
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:
local player = game:GetService('Players').PlayerAdded:Connect(function(player) local char = player.CharacterAdded:Connect(function(character) local hum = character:WaitForChild("Humanoid") local repstorage = game:GetService("ReplicatedStorage") local DeathEvent = repstorage:WaitForChild("DeathEvent") hum.Died:connect(function() print("player has died! detected from server") DeathEvent:FireAllClients(player) end) end) end)
On your local script, you can just use
local player = game.Players.LocalPlayer
instead, as it is in a local script. Otherwise, it should work.
Here is your edited local script:
local localplayer = game:GetService('Players').LocalPlayer local char = localplayer.Character local hum = char:WaitForChild("Humanoid") local DeathEvent = game.ReplicatedStorage.DeathEvent local livestext = "lives: " local numberoflives = 3 local lives = livestext .. numberoflives local text = script.Parent.Text DeathEvent.OnClientEvent:Connect(function(player) --condenses the creation of the function and connects it to the event in the same line if player == localplayer then --Check if the player mentioned in the event is the same as the gui owner... print("player has died! remote event worked") text = "is it working" end 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)
local localplayer = game:GetService('Players').LocalPlayer local char = localplayer.Character local hum = char:WaitForChild("Humanoid") local DeathEvent = game.ReplicatedStorage.DeathEvent local livestext = "lives: " local NumberOfLives = 3 local livesGui = script.Parent DeathEvent.OnClientEvent:Connect(function(player) --condenses the creation of the function and connects it to the event in the same line if player == localplayer and NumberOfLives > 0 then --Check if the player mentioned in the event is the same as the gui owner... livesGui.Text = livestext .. tostring(NumberOfLives - 1) NumberOfLives = NumberOfLives - 1 elseif player == localplayer and NumberOfLives <= 0 then livesGui.Text = "you have no more lives" end end)
in my game once the player has no more lives I will teleport them.