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)
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.