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

Why will a piece of text not change if a value linked to it via script changes?

Asked by
2Loos 168
4 years ago

What I would like to know: A. Why this happens B. Ways to fix it C. Is there anything I should remember about changing text using constantly changing values

My Script:

01local ServerStorage = game:GetService("ServerStorage")
02local Players = game:GetService("Players")
03 
04 
05game.Players.PlayerAdded:Connect(function(player)
06    local kills = game.Players[player.Name].leaderstats.Kills.Value
07    local deaths = game.Players[player.Name].leaderstats.Deaths.Value  
08    local killsVal = game.Players[player.Name].leaderstats.Kills
09    local deathsVal = game.Players[player.Name].leaderstats.Deaths 
10 
11 
12 
13 
14 
15 
View all 39 lines...

If you are wondering what this script is doing, I made a GUI that floats above a player's head showing their Kills and Deaths. Obviously, the value is changing a lot, and I am clueless as to why the text won't change. I have tried using the following code inside the script:

01killsVal.Changed:Connect(function(NewValue)
02        killsVal.Value = kills
03    end)
04    deathsVal.Changed:Connect(function(NewValue)
05        deathsVal.Value = deaths
06    end)
07 
08    local function onValueChanged()
09        if kills.Value ~= killsVal then
10            kills.Value = killsVal
11        end
12        if deaths.Value ~= deathsVal then
13            deaths.Value = deathsVal
14        end
15    end

but was only lead to a bunch of errors including this "leaderstats is not a valid member of Player." I would be grateful for any help.

2 answers

Log in to vote
0
Answered by 4 years ago

You are only changing the text label text once, which is when the character is added. You need to make it so your text changes whenever the value changes, for instance...

01killsVal.Changed:Connect(function(NewValue)
02        killsVal.Value = kills
03    end)
04    deathsVal.Changed:Connect(function(NewValue)
05        deathsVal.Value = deaths
06    end)
07 
08    local function onValueChanged()
09        if kills.Value ~= killsVal then
10            kills.Value = killsVal
11        clonedgui2.Textlabel.Text = "Kills: "..kills
12        end
13        if deaths.Value ~= deathsVal then
14            deaths.Value = deathsVal
15        clonedgui1.Textlabel.Text = "Deaths: "..deaths
16        end
17    end
Ad
Log in to vote
0
Answered by
2Loos 168
4 years ago

I'm confused about this... I created a local function but it doesn't even bother to start up, and I don't know how to make it run. Excuse my blunt terminology, I am a beginner scripter.

01local function StatChange(player)
02 
03    local stats = game.Players[player.Name]:WaitForChild("leaderstats")
04    local kills = stats.Kills.Value
05    local deaths = stats.Deaths.Value  
06    local killsVal = stats.Kills
07    local deathsVal = stats.Deaths
08    local clonedgui1 = game.Players(player.Name).Head.Deaths.clonedgui1.TextLabel
09    local clonedgui2 = game.Workspace:WaitForChild(player.Name).Head.Kills.clonedgui2.TextLabel
10 
11    kills.Changed:Connect(function()
12        killsVal.Value = kills
13        clonedgui2.Text = killsVal.Value
14    end)
15 
View all 22 lines...

Answer this question