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

Script is returning "Attempt to compare number <= nil"?

here is the code:

local sound = script.Parent.Money
local cost = 0
script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
	    if  _G.Money >= cost then
		sound:Play()
		Instance:Remove(script.Parent)
		end	
	end
end)

heres the money variable:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Value = 0
	Money.Parent = leaderstats
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

2 comments

0

You are getting this error because _G.Money does not exist.

0

Are you sure u have " _G.Money" as an variable if u have and still have this error u could try and warp in tonumber()

1 answer

✓ Accepted answer

I do not recommend using _G because hackers might easily access the variable and get infinite money. Instead, get money from the leaderstats. The first script should be like this:

local sound = script.Parent.Money
local cost = 0
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
local player = game.players:GetPlayerFromCharacter(hit.Parent)
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats then
local MoneyValue = leaderstats:FindFirstChild(“Money”)
if MoneyValue then
if MoneyValue.Value  >= cost then
MoneyValue.Value = MoneyValue.Value - cost
sound:Play()
script.Parent:Destroy()
end
end
end
end
end)

4 comments

0

This solved it, but now im noticing the audio won't play when you press the button.

0

nevermind, turns out it needed to be in the workspace

0

_G is different for client and server, hacker will not be able to modify server _G variables.

0

Oh, thanks for the information. I saw an article saying that _G might be used by hackers in the DevForum. But my solution is still working.

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