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
You're getting the StarterGui, these changes will not show until you reset and respawn. StarterGui is not the PlayerGui.
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.
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)
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.