Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
Edited 2 years ago

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)
0
You are getting this error because _G.Money does not exist. ROMAHKAO 35 — 2y
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() gamingwithjbthepro 76 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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)

0
This solved it, but now im noticing the audio won't play when you press the button. MrBucketOfficial 39 — 2y
0
nevermind, turns out it needed to be in the workspace MrBucketOfficial 39 — 2y
0
_G is different for client and server, hacker will not be able to modify server _G variables. ROMAHKAO 35 — 2y
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. T3_MasterGamer 2189 — 2y
Ad

Answer this question