The problem with the script

Hello everyone, I have such a problem in the script that it gives such an error, I don’t know what to do. the script runs every other time. I hope for your help!

local modulScript = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
    local randomGold = modulScript.randomGold
	local gui = script.Parent.Parent
	local text123 = gui:WaitForChild("TextLabel1")
	text123.Text = randomGold
	
	local function changeGems()
		local abbreviations = {
			K = 4,
			M = 7,
			B = 10
		}

		local text = tostring(math.floor(randomGold))
		local chosenAbbreviation
		for abbreviation, digits in pairs(abbreviations) do
			if #text >= digits and #text < (digits + 3) then
				chosenAbbreviation = abbreviation
			end
		end

		if chosenAbbreviation then
			local digits = abbreviations[chosenAbbreviation]
			local rounded = math.floor(randomGold / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
			text123.Text = string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbreviation
		else
			text123.Text = text
		end	
	end

	changeGems()
	
local module = {
	randomGold = math.random(200000, 250000),
	randomGem = math.random(2, 10)
}

return module

You need to use tostring() to convert ‘randomGold’ to text.

textLabel.Text = tostring(randomGold)

Doesn’t explain why his error message says it’s nil. You can set the Text property to a number btw.

I tried your code and it works just fine for me. I bet that you have multiple instances called “ModuleScript” in replicated storage, and the one it’s using doesn’t have a randomGold key.

thanks bro!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.