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

Text in Gui won't change?

Asked by 8 years ago
local gui = game.StarterGui.PointsGui.PointsFrame.Points

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local number = 0
        number = number + 1
        gui.Text = number
    end
end

script.Parent.Touched:connect(onTouched)

What it's supposed to do: There is a gui on the left side of the screen that says how many points you have, and every time you touch this brick, the points(the text in the gui) is supposed to go up. It isnt giving me an error message, it just doesnt work

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

You're getting the StarterGui, these changes will not show until you reset and respawn. StarterGui is not the PlayerGui.


Solution

You need to access the PlayerGui. PlayerGui will be located in the actual player. Once you change something in the PlayerGui, then you will see the changes. The variable number will also only be one since you keep replacing whatever the value is to 0 each time someone hits the brick.


Final Script

function onTouched(hit)
    local PlayerWhoHit = game.Players:GetPlayerFromCharacter(hit.Parent) --Set up a variable to find if someone hit the brick.
    if PlayerWhoHit then --PlayerWhoHit will either have a value or have nil. If it has nil, then the if then statement will be skipped.
        local gui = PlayerWhoHit.PlayerGui.PointsGui.PointsFrame.Points
        --local number = 0 --The number would be 1 each time you hit the brick, you can not have this variable inside the function. I also assume you're trying to up the number of the single player, so we just need to take the number from their gui and put it in here.
        local number = tonumber(gui.Text)
        if number == nil then number = 0 end --This is just in case the text of Points text is anything but a number.
        number = number + 1 --This line really isn't necessary, you can just do gui.Text = number + 1 on the next line, but I'll keep it around.
        gui.Text = number
    end
end

script.Parent.Touched:connect(onTouched)

Hopefully this answered your question, if it did do not forget to hit the accept answer button. If you have any questions, feel free to comment below and I will get back to you.
0
I've never seen a better explanation before, thank you so much!! Squidier 40 — 8y
0
If you dont mind me asking though, what does line number 06 mean? (local number = tonumber(gui.Text) Squidier 40 — 8y
1
tonumber converts a string to a number, if possible. So "25" becomes 25. If the string is something like "Hello!", however, tonumber will just return nil, which is what line 7 is for. Perci1 4988 — 8y
0
It's changing the string gui.Text into a number. This will result in one of two things, if the string actually is a number it will return that number. Otherwise if you have something like "123a", then the tonumber function will return nil as there is that "a" in there. I have on line 7 to check if the number is actually a number or not. M39a9am3R 3210 — 8y
0
Thank you! And Im really sorryu :| but could you also tell me how to get it so it doesnt just give me one point? Squidier 40 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Your script is trying to access the StarterGui on this line :-

local gui = game.StarterGui.PointsGui.PointsFrame.Points

You need to access the players GUI e.g (this is from the wiki):-

-- Accessing PlayerGui from a Server Script:
game.Players.PlayerName.PlayerGui

-- Accessing PlayerGui from a LocalScript:
game.Players.LocalPlayer.PlayerGui

Edit:-

local gui = game.StarterGui.PointsGui.PointsFrame.Points -- replace this as show above as you need to reference the players gui

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

    -- The second problem is here
        local number = 0 -- you create a new variable number and set it as 0
        number = number + 1 -- then you add 1
        gui.Text = number -- then you set this as 1 meaning that it will not change from 1

    end
end

script.Parent.Touched:connect(onTouched)

This is one way you could solve it (change the variable scope):-

local gui = game.StarterGui.PointsGui.PointsFrame.Points -- replace this as show above as you need to reference the players gui

 local number = 0 -- the variable is now outside so it will not be created inside the function, this means it will exist after the function finishes allowing you to add 1 to it. 

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

        number = number + 1 -- then you add 1
        gui.Text = number -- then you set this as 1 meaning that it will not change from 1

    end
end

script.Parent.Touched:connect(onTouched)

This is just one way you could do it and this should only be used as an example as I do not know your game plan/design.

0
And after PlayerGui I'd be able to continue like normal with the PointsGui.PointsFrame.Points/ Squidier 40 — 8y
0
Players.PlayerName didnt work, it says that PlayerName is not a valid member of Players Squidier 40 — 8y
0
Yes just replace line 1 to access the gui. You also need to find number as the var is created each time, i will edit the question to show you User#5423 17 — 8y

Answer this question