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
3 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:

local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")


game.Players.PlayerAdded:Connect(function(player)
    local kills = game.Players[player.Name].leaderstats.Kills.Value
    local deaths = game.Players[player.Name].leaderstats.Deaths.Value   
    local killsVal = game.Players[player.Name].leaderstats.Kills
    local deathsVal = game.Players[player.Name].leaderstats.Deaths  






    player.CharacterAdded:Connect(function(character)

        local clonedgui1 = game.ServerStorage.Deaths:Clone()
        clonedgui1.TextLabel.TextColor3 = Color3.fromRGB(255,0,0)
        clonedgui1.Parent = game.Workspace:WaitForChild(player.Name).Head           
        clonedgui1.TextLabel.Text = "Deaths: "..deaths




        local clonedgui2 = game.ServerStorage.Kills:Clone()
        clonedgui2.TextLabel.TextColor3 = Color3.fromRGB(0,255,0)
        clonedgui2.Parent = game.Workspace:WaitForChild(player.Name).Head
        clonedgui2.TextLabel.Text = "Kills: "..kills






        end)


end) 

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:

killsVal.Changed:Connect(function(NewValue)
        killsVal.Value = kills
    end)
    deathsVal.Changed:Connect(function(NewValue)
        deathsVal.Value = deaths
    end)

    local function onValueChanged()
        if kills.Value ~= killsVal then
            kills.Value = killsVal
        end
        if deaths.Value ~= deathsVal then
            deaths.Value = deathsVal
        end
    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 3 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...

killsVal.Changed:Connect(function(NewValue)
        killsVal.Value = kills
    end)
    deathsVal.Changed:Connect(function(NewValue)
        deathsVal.Value = deaths
    end)

    local function onValueChanged()
        if kills.Value ~= killsVal then
            kills.Value = killsVal
        clonedgui2.Textlabel.Text = "Kills: "..kills
        end
        if deaths.Value ~= deathsVal then
            deaths.Value = deathsVal
        clonedgui1.Textlabel.Text = "Deaths: "..deaths
        end
    end
Ad
Log in to vote
0
Answered by
2Loos 168
3 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.

local function StatChange(player)

    local stats = game.Players[player.Name]:WaitForChild("leaderstats")
    local kills = stats.Kills.Value
    local deaths = stats.Deaths.Value   
    local killsVal = stats.Kills
    local deathsVal = stats.Deaths
    local clonedgui1 = game.Players(player.Name).Head.Deaths.clonedgui1.TextLabel
    local clonedgui2 = game.Workspace:WaitForChild(player.Name).Head.Kills.clonedgui2.TextLabel

    kills.Changed:Connect(function()
        killsVal.Value = kills
        clonedgui2.Text = killsVal.Value
    end)

    deaths.Changed:Connect(function()
        deathsVal.Value = deaths
        clonedgui1.Text = deathsVal.Value
    end)    


end

Answer this question