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

Code main fuction calls error. Possible fix?

Ok so I'm making a code gui. It was working until I introduced a code that has a limit to it. Now that there is a limit it just breaks.

The error im getting is :

ServerScriptService.Codes:12: attempt to index local 'CheckUsage' (a nil value)
Stack Begin
Script 'ServerScriptService.Codes', Line 12 - upvalue CheckLimited
Script 'ServerScriptService.Codes', Line 38
Stack End

My code for reference:

local ActiveCodes = game.ServerStorage.ActiveCodes

local Remote = game.ReplicatedStorage.Code

local Data = game:GetService("DataStoreService")
local Getcode = Data:GetDataStore("UsedCode")
--local Get = Getcode:GetAsync(Plr.UserId .. Code)

local function CheckLimited(CodeObj)
	local CheckUsage = Getcode:GetAsync("GlobalUseOf" .. CodeObj.Name)
	if CheckUsage == nil then
		CheckUsage:SetAsync("GlobalUseOf" .. CodeObj.Name, 0)
		return 0
	else
		return CheckUsage
	end
end

local function SaveLimited(CodeObj)
	local GetUsage = Getcode:GetAsync("GlobalUseOf" .. CodeObj.Name)
	if GetUsage ~= nil then
		GetUsage:SetAsync("GlobalUseOf" .. CodeObj.Name, GetUsage + 1)
	end
end

local function OnCode(Plr, Code)
	local Get = Getcode:GetAsync(Plr.UserId .. Code)
	
	local Stats = Plr:WaitForChild("leaderstats")
	local CodeObj = game.ServerStorage.ActiveCodes:FindFirstChild(Code)
	
	if not CodeObj then return "No active code called " .. Code end
	
	local RequiredStat = Stats:WaitForChild(CodeObj.Stat.Value)
	local CodeAmmount = CodeObj.Value
	
	if CodeObj:FindFirstChild("Limit") then
		if CheckLimited(CodeObj) < 10 then
			if Get == nil then
				if RequiredStat.Name == "Shards" then
					RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats.Rebirths.Value + 1))
				elseif RequiredStat.Name == "Rebirths" then
					RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Super Rebirths"].Value + 1))
				elseif RequiredStat.Name == "Super Rebirths" then
					RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Ultra Rebirths"].Value + 1))
				elseif RequiredStat.Name == "Ultra Rebirths" then
					RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Omega Rebirths"].Value + 1))
				elseif RequiredStat.Name == "Omega Rebirths" then
					RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Master Rebirths"].Value + 1))
				else
					RequiredStat.Value = RequiredStat.Value + (CodeAmmount)
				end
				SaveLimited(CodeObj)
				Getcode:SetAsync(Plr.UserId .. Code, true)
			else
				return "Code already Used"
			end
		else
			return "Code Limit Reached"
		end
	else
		if RequiredStat.Name == "Shards" then
			RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats.Rebirths.Value + 1))
		elseif RequiredStat.Name == "Rebirths" then
			RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Super Rebirths"].Value + 1))
		elseif RequiredStat.Name == "Super Rebirths" then
			RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Ultra Rebirths"].Value + 1))
		elseif RequiredStat.Name == "Ultra Rebirths" then
			RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Omega Rebirths"].Value + 1))
		elseif RequiredStat.Name == "Omega Rebirths" then
			RequiredStat.Value = RequiredStat.Value + (CodeAmmount * (Stats["Master Rebirths"].Value + 1))
		else
			RequiredStat.Value = RequiredStat.Value + (CodeAmmount)
		end
		Getcode:SetAsync(Plr.UserId .. Code, true)
	end
end

Remote.OnServerInvoke = OnCode

Hope someone can help me!

2 comments

0

I think you should check line 12 again....idk

0

Ye i just realised thanks

2 answers

Your if statement on line 11 is exactly the wrong way around:

if CheckUsage == nil then

You have to use ~= to check if CheckUsage is not nil

2 comments

0

Nope I'm checking to see if its nil so I can set it to 0.

0

Wait i just realsied an error ive had lol

Turns out i didn't recheck so sorry to anyone who took the time.

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