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

How do I change the text of a gui via script when something specific happens?

Asked by
Kitorari 266 Moderation Voter
7 years ago
Value = workspace.Partscan.Sugar.Value

if workspace.Partscan.Sugar.Value == 0 then
    workspace.Partno.SurfaceGui.Frame.Sugar.Text = "Sugar 0/1"
elseif workspace.Partscan.Sugar.Value == 1 then
    workspace.Partno.SurfaceGui.Frame.Sugar.Text = "Sugar 1/1"
end

The Text works when there's no sugar, but when sugar is added, it doesn't function. Not sure what to do.

Can someone please help me? thanks!

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Why your code only works once

It is only ran once so the 1st result will always be the value of sugar.

What you need is to add a changed event so when the value of sugar changes the Gui is updated.

local sugarObj = game.Workspace.Partscan.Sugar

sugarObj .Changed:connect(function(newVal) -- this is the changed event for the sugar which gives the new value as a parameter

-- set the new value
game.Workspace.Partno.SurfaceGui.Frame.Sugar.Text =  "Sugar " .. tostring(newValue) .. "/1" 
end)

This is only an example, hope this helps

How to improve this script

Currently there are no checks to there is a possibility of parts being nil

Ad
Log in to vote
1
Answered by 7 years ago

Hey Kitorari,

Ok so basically what you want to do is use game.Workspace, it will help. And you are using numbers with integers (but that isnt the real problem I dont think).

Value = tonumber(game.Workspace.Partscan.Sugar.Value)
SugarLabel = game.Workspace.Partno.SurfaceGui.Frame.Suger

if Value == 0 then
    SugarLabel.Text = "Sugar 0/1"
elseif Value == 1 then
   SugarLabel.Text = "Sugar 1/1"
end

If this works, accept answer, it helps both of us

0
Actually, though its more simplier, the other guy made it a function, so it worked multible times Kitorari 266 — 7y
0
ah ok RadioactiveSherbet 30 — 7y

Answer this question