If statement issue

From reading the documentation linked, and from experience. If you look into a lot of the open source modules, free models, etc. when learning you can see a lot of different coding styles and choices that are all syntactically correct.

Every time you encounter something new or strange, look it up in the documentation or ask the creator, and you end up with a wide breadth of knowledge.

If you only ever follow 1 person’s tutorials, and never look at code beyond that, then you’ll only ever know that one person’s style of coding.

I think I still prefer using brackets because it’s already my style. But thanks.

2 Likes

Hi, as the others already said, it must be PlayerGui, it must not necessarily be in a LocalScript.

function checktest(Player)
	print("checktest 2")
    if Player.PlayerGui.Value.Value == 2 then
	    print("checktest 3")
    end
end

It only remains to call the function by giving it the player.
Example:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
    checktest(Player)
end)

I think there’s a lot of confusion in the comments so I’ll just start from scratch here.

Set up your StarterGui hierarchy like this (if you want it to be in StarterGui). There’s potentially the discussion for having the value in ReplicatedStorage and the script in StarterPlayerScripts, but let’s go one step at a time and get your code working first.

image

The contents of the LocalScript:

local numberValue = script.Parent:WaitForChild( 'YourValue' )

local function checktest()
	print( 'The value is:', numberValue.Value )
end

numberValue.Changed:Connect( checktest )
checktest()

You can then go on to implement your if statements, or your GUI changes, or whatever you would like to occur when the value changes.

Use relative paths to get to your GUI if it’s in StarterGui too - that way you don’t need to worry about finding the player and getting the PlayerGui folder. Relative paths are simpler if you know the script is always going to have the same parent as the GUI.


Edit for additional clarity: This is client-sided, suitable as a listener for changes to the value to then perform GUI updates. You will likely want a server-sided Script to set the value. Please change script.Parent.YourValue to appropriately reflect the path to your value. E.g. if your value is called Cash, and is in the same hierarchical location as the example, script.Parent.Cash.

Print can work without brackets

I know, they already explained.

Is the script you showed from the server or client?

It errors in output which states your value is not a member of PlayGui

It’s implied that it’s client side by the fact it’s a LocalScript and will be running inside of the PlayerGui as you can see I’ve placed the LocalScript inside StarterGui.

OP mentioned their use case was to update a GUI based on changes in the value, so a LocalScript is most appropriate.

I would expect a server-sided Script to perform the actual value changes, but what we have here is effectively the listener, which should be client-based for what OP wants to do with it.

Can you show your output and a screenshot of the StarterGui, because if you set it up how I’ve shown, it categorically does not produce that error.