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

How do I add to a text button's number?

Asked by 3 years ago
Edited 3 years ago

How would I add a certain amount, (lets say for this example 1), to a TextLabel inside a startergui after the player touches a block? This is my code right now:

local val = 0
function onTouch(hit)
    local num = val + 1
    game.StarterGui.Cashcollected.cash.Text = num
end


script.Parent.Touched:connect(onTouch)

it doesn't work from what I'm seeing, which sucks. I'm not sure how to fix it unfortunately. Any help would be greatly appreciated! (THE CODE IS INSIDE THE BLOCK BTW)

1 answer

Log in to vote
0
Answered by 3 years ago

Alright so what you're doing here is change the textlabel in StarterGUI. This is a common mistake as StarterGUI does not change the textlabel which is replicated in the player. Let me tell you how it works

When a player joins your game, the game copies and pastes the GUI which is in StarterGUI to something known as a PlayerGUI. This place is where the GUI will be seen by the player. Think of it like StarterGUI is where the GUI is stored and PlayerGUI as the screen where the GUI will be displayed

Instead of changing the textlabel inside of StarterGUI, you need to change the textlabel which is in PlayerGUI.

Also your script should be a localscript if it isn't already. Im not sure is Touched is able to activate locally though, so please inform me if it doesn't work.

function onTouch(hit)
    local num = val + 1
    local LocalPlayer = game.Players.LocalPlayer -- This refers to the player playing the game. 
    local PlayerGUI = LocalPlayer:FindFirstChild("PlayerGui") -- This is the PlayerGui
    PlayerGui.Cashcollected.cash.Text = num
end

If there are any errors feel free to commend as im currently typing this on a laptop and have not tested out the script yet.

0
It didn't work. I tried my best to change it around to fix it but I believe it just doesn't work because it's a local script. If that's not the problem then I'm just not sure. CameUpFromNothing 37 — 3y
0
Aight, try adding a normal script and link both of them using remoteevents. So when the part gets touched by the player, the serverscript (regular script) will get informed and notify the client using the remoteevent. I'll edit my post to show you how it would look like. therealrobloxianman2 0 — 3y
0
Editing is broken - On the regular script do the same 'onTouch' function and instead of attempting to change the GUI, we're gonna leave that to the client. This may cause security issues in the future if your game is ridden with exploiters, but it should do the trick until then. So replace all of the code with 'local player = game:GetService("Player")... therealrobloxianman2 0 — 3y
0
...local char = player:GetPlayerFromCharacter(hit.Parent) if char then game.[Place where the RemoteEvent is]:FireClient(char) end' and on your localscript keep all of the code i written on the original post and add 'game.[Place where the RemoteEvent is].OnClientEvent:Connect(onTouch())' therealrobloxianman2 0 — 3y
Ad

Answer this question