Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why Won't my Image Show When Leaderstats Is Enough? [Reupload]

I had to reupload this post because I wasn't getting any answers that worked. So, I want to make a collecting game and the catalog shows the collectibles you have. I made all of the collectibles leaderstats so you can save it. So, I made the image in the gui have a value and a script. Here it is:

local ting = 0
mlvl = script.Parent.Lvl.Value
if ting == 0 then
	ting = 1
	if check ~= nil then
		local user = game.Players.LocalPlayer
		local stats = user:findFirstChild("leaderstats")
		local Lvl = stats.Molly
		if stats ~= nil and Lvl.Value >= mlvl then
			script.Parent.Parent.Molly.ImageTransparency = 1
		end
		if Lvl.Value < mlvl then
			script.Parent.Parent.Molly.ImageTransparency = 0
		end 
	end
	ting = 0
end

So if you have this much leaderstat, the image will show up, basically. So, the problem is, the image doesn't show up. Thank you!

8 comments

0

is this the full script? and is it a local script

0

@kingdom5, Yes, this is the full script and it's a server script. I have tried local and server script.

0

Then where do I see you defining 'check'?

0

@Feahren, What do you mean? And I didn't made this script exactly. I did do the stuff after "if check" though.

0

The first issue is that this is not inside an changed event so when Lvl changes the code runs the checks again

0

@Kindom5 How do I do that?

0

you haven't defined what "check" is. What do you mean by "check"?

0

@kingblaze, I’m not sure, like I said, I didn’t code that part...

2 answers

Firstly I would discouraged the use of Value Object to store player data in during gameplay. Using a table is far more flexable and with modules sharing data is not an issue. You can then use a RemoteEvent to let the player know what collectibles they have.

Back to your question...

When dealing with GUIs generally they are going to be on the client side so would need to be a local script. As for the code itself it will only be ran once on load. You want to look at using the Changed event of Value Objects which will run upon the value changing and passes the new value.

Take note that local scripts may run before parts of the game load fully use WaitForChild where applicable.

Example

local mlvl = ??

-- use get service as the service name can be changed
local localPlr = game:GetService('Players').LocalPlayer

-- wait for the players leaderstats to become available
local leaderstats = localPlr:WaitForChild('leaderstats')

local molly= leaderstats:WaitForChild('Molly')
local mollyGuiItem = script.Parent.Parent:WaitForChild('Molly') -- gui

-- connect a changed event to the value object
Molly.Changed:Connect(function(newVal) -- the new value passed
	-- Research into Lua ternary operator to understand this in more detail. 
	-- Molly.ImageTransparency = newVal < mlvl and 1 or 0

	if newVal < mlvl then
		mollyGuiItem .ImageTransparency = 1
	else
		mollyGuiItem .ImageTransparency = 0
	end
end)

This is just a general example of how this can be done though there will be more efficient ways to do this on scale.

I hope this helps. Please comment if you have any other question about the code.

4 comments

0

Thank you for replying, but unfortunately, this didn't work...

0

Also, what’s a table?? You mentioned that you discouraged it.

0

@ PadmeOragana You should review the basics of Roblox Lua and look into what tables are. The code above is an example it will not run out of the box

0

@Kingdom5, Well then what about the example? It doesn't work.

This discussion is preserved as it was. New answers, comments, and votes are closed.