Hello. I have a lives system for my game, and have also made a lives gui that I want to reflect the player’s remaining lives in. I am wondering how I would call my lives value from my script in serverscript service into a localscript in my lives gui so the lives bar can update. I am assuming that it would be a remote event, but I am not really sure how to write it.
This is the lives script in SSS
local function onPlayerAdded(player)
local Lives = Instance.new("IntValue", player)
Lives.Value = 5 <----- this is the value I want to reference in my gui
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local function onDied()
if Lives.Value > 0 then
task.wait(RespawnDelay)
Lives.Value -= 1
print("Losing Life!")
player:LoadCharacter()
else
print("No Lives Left!")
end
end
This is my lives bar:
What I am trying to do is make it so when the lives value decreases by 1 on death the lives bar reflects that by changing the heart farthest to the right to this
in order to indicate the player lost a life, but I am not sure how to reference the lives value I have in the SSS script in a localscript correctly.
For reference, this is located in startergui:
Life1 - 5 is the red hearts from left to right, and HC1 - 5 is the darker hearts from left to right which are currently set as invisible. I’m simply trying to just make the Life hearts invisible and the respective HC visible when a life is lost.
This is just a small example on how you wanna send the Lives value to the client:
Server Script
-- Server Script
local RemoteEvent = game.ReplicatedStorage:WaitForChild("PlayerDied")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
char.Humanoid.Died:Connect(function()
warn("player died")
Lives.Value -= 1
RemoteEvent:FireClient(player, Lives.Value) -- Send the data to client
end)
end)
end)
Local Script in your client
-- Local Script
local function changeGui(newVal)
print(newVal) -- use this number to recolor/retexture the hearts/lives in your gui
end
game.ReplicatedStorage:WaitForChild("PlayerDied").OnClientEvent:Connect(changeGui)
Many possible ways to recolor/retexture the hearts when player got the client fired event of the remote, here’s just a basic example
-- Local Script
local function changeGui(newVal)
print(newVal) -- use this number to recolor/retexture the hearts/lives in your gui
-- Inside a frame theres 3 frames simulating the hearts
-- Im recoloring all hearts to red first
for _, h in pairs(script.Parent.Frame:GetChildren()) do
if h:IsA("Frame") then
h.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- RED
end
end
--[[
then on a loop recoloring the amount of lives gotten from server
the frames are named with numbers 1,2,3. So each iteration recolor the frame to green
at the end only recolors the amount of lives the player still has
]]
for c = 1, newVal do
script.Parent.Frame:FindFirstChild(c).BackgroundColor3 = Color3.fromRGB(60, 255, 1) -- GREEN
end
end
game.ReplicatedStorage:WaitForChild("PlayerDied").OnClientEvent:Connect(changeGui)
Sorry for the late response. I tried your code and it seems to work, but I have a question.
Why does it only briefly change the color of the lives indicator and then reset it when I respawn? Is it some sort of client sided problem?
Here is the client script I used, just a slightly edited version of your example:
local function changeGui(newVal)
print(newVal)
for _, h in pairs(script.Parent.LivesBar:GetChildren()) do
if h:IsA("ImageLabel") then
h.ImageColor3 = Color3.fromRGB(66, 0, 0)
end
end
for c = 1, newVal do
script.Parent.LivesBar:FindFirstChild(c).ImageColor3 = Color3.fromRGB(170, 0, 0)
end
end
game.ReplicatedStorage:WaitForChild("LivesRemoteEvent").OnClientEvent:Connect(changeGui)
Your code works fine, just make sure you disabled the option “Reset On Spawn” in your ScreenGui properties, otherwise everytime the player spawns the gui is reset to the initial state (the 5 hearts turned on)