Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I go about making this "Life Counter" GUI?

Asked by 5 years ago
Edited 5 years ago

Hello. I currently have a localscript inside the player that runs upon death. I have a GUI in the startergui that displays the amount of deaths you are allowed before changing your respawn to the start of the level . My problem arises because the GUI refuses to change the Text value, even when I set it to just "Sample" as a part of debugging. Also, no errors are displayed in the output with my testing.

script.Parent.Humanoid.Died:Connect(function(dead)
local Life = game.StarterGui.Lives.CurrentLives.Value
local Text = game.StarterGui.Lives.TextLabel.Text
Life = Life - 1
Text = "X " ..Life
print(Life)
if Life < 0 then
    game.ServerStorage.Magna.PrimaryPart.Position = Vector3.new(-36.176, 150.398, 851.044)
    Life = 3
end
end)
0
Use a Number Value and the .Changed Event The_Pr0fessor 595 — 5y
0
Where is this script located? User#24403 69 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

Well, there are a couple of problems that you currently have in your code:

  • referencing the startergui instead of the playergui

  • trying to access the server storage from a local script

  • trying to change a value and have the change show up on an object

With that said, we can get to fixing your code

With the first problem, instead of referencing the StarterGui, we need to reference the playerGui which is a child of the player object so we can get it by doing:

local plrgui = game.Players.LocalPlayer.PlayerGui

Now for the second problem, you can circumvent this by simply moving the objects in question from ReplicatedStorage . However, as I don't really know what you are trying to accomplish, but I'd like to leave a warning that setting the position of anything in a local script doesn't change it for the server.

With that said, here is the fixed code:

local plr = game.Players.LocalPlayer
local rep = game.ReplicatedStorage
local plrgui = plr.PlayerGui
script.Parent.Humanoid.Died:Connect(function(dead)
    local Life = plrgui.Lives.CurrentLives
    local Text = plrgui.Lives.TextLabel
    Life.Value = Life.Value - 1
    Text.Text = "X " ..Life.Value
    print(Life)
    if Life.Value < 0 then
        rep.Magna.PrimaryPart.Position = Vector3.new(-36.176, 150.398, 851.044)
        Life.Value = 3
    end
end)

Addition: I realized that I forgot to explain the 3rd problem originally, so here it is:

What you are by defining life and text like that makes them fixed values instead of references Thus any change to those variable doesn't show up on the textlabel or int value.

I believe incapaxx's answer here provides a great explanation on the topic

Hopefully this helped, have a nice day!

0
Thank you! This really helped. I appreciate how in-depth you went with your explanation, too. It means a lot. krankycaat 11 — 5y
0
thanks for the feedback :) theking48989987 2147 — 5y
0
Enjoy the Thousand, you deserved my upvote! Ziffixture 6913 — 5y
0
I would also like to mention that I am using a custom character loader, and as such the position in which you spawned is determined by the model's position when you copy it to the ServerStorage. krankycaat 11 — 5y
Ad

Answer this question