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

Attempted to index global value which = nil-Money script, help?

Asked by
unmiss 337 Moderation Voter
9 years ago

I honestly feel bad already by uploading this to the site as I'm using it for a scripting application (as he didn't want my Courthouse tech for some reason). I don't know if I'll delete it after or not.

I guess I can't use these values because they're in a different function. I've commented on each line so you understand.

Error:

Players.Player.PlayerGui.MoneyScript:34: attempt to index global '(one of the variables that are there, sometimes random, sometimes not based on what is there and not)' (a nil value)

IGNORE there's missing lines at the top. There's a comment I didn't include.

Code:


-- Variables game.Players.PlayerAdded:connect(function(plr) MPM = Instance.new("NumberValue", game.ReplicatedStorage) -- Money Per Minute (not to be edited) MPM.Name = plr.Name.."_MPM" MPM.Value = 100 CM = Instance.new("NumberValue", game.ReplicatedStorage) -- Current Money (could be edited, for starting money.) CM.Name = plr.Name.."_CM" CM.Value = 0 MPMMultiplier = Instance.new("NumberValue", game.ReplicatedStorage) -- Money Per Minute Multipler (should be edited, also edited by the buy button) MPMMultiplier.Name = plr.Name.."_MPMMultiplier" MPMMultiplier.Value = 1 FMPM = Instance.new("NumberValue", game.ReplicatedStorage) -- Multipled MPM and MPMMultipler. No need to touch this at all. FMPM.Name = plr.Name.."_FMPM" end) CMText = script:WaitForChild("MoneyGui").Frame.CurrentMoney while wait(1) do -- usually 60, but for testing purposes is 1 FMPM.Value = (MPM.Value * MPMMultiplier.Value) -- Multiplies MPM (100) by the MPM Multiplier CM.Value = (CM.Value + FMPM.Value) -- Adds the Money Per Minute to the Current Money CMText.Text = CM.Value -- Quite simply changes the text that the user can see, their money, to the Value of CMValue print("Money added") end

I tried using ACTUAL value objects placed in there but it just wasn't working right and was annoying me a lot. So I switched to doing it in a script. I don't know how I'll do functions for adding to the value or anything so that it doesn't interfere :(

ANY SOLUTIONS to this situation and fixes to what I should do to this script? This has to be perfect, and I'm STUMPED.

0
Could you make prints on every line to see where it stops? drew1017 330 — 9y
0
Not the issue. unmiss 337 — 9y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

PlayerAdded events don't tend to work in LocalScripts, which I assume this is since it's messing with GUI stuff. Additionally, every new player that joins will break every pre-existing player, since you overwrite the variables.

Your actual error is caused because you're trying to access a value you assume to exist, while it is still set to nil. To fix this whole mess, I would split this all into two scripts: one to handle adding money (in ServerScriptService), and one to update the GUI when the value changes:

--Script in ServerScriptService

game.Player.PlayerAdded:connect(function(plr)
    local MPM = Instance.new("NumberValue", game.ReplicatedStorage)
    MPM.Name = plr.Name .. "_MPM"
    MPM.Value = 100

    local CM = Instance.new("NumberValue", game.ReplicatedStorage)
    CM.Name = plr.Name .. "_CM"
    CM.Value = 0

    local MPMMultiplier = Instance.new("NumberValue", game.ReplicatedStorage)
    MPMMultiplier.Name = plr.Name .. "_MPMMultiplier"
    MPMMultiplier.Value = 1

    local FMPM = Instance.new("NumberValue", game.ReplicatedStorage)
    FMPM.Name = plr.Name .. "_FMPM"
end)

while true do
    for _, plr in ipairs(game.Players:GetPlayers()) do
        local MPM = game.ReplicatedStorage:FindFirstChild(plr.Name .. "_MPM")
        local CM = game.ReplicatedStorage:FindFirstChild(plr.Name .. "_CM")
        local MPMMultiplier = game.ReplicatedStorage:FindFirstChild(plr.Name .. "_MPMMultiplier")
        local FMPM = game.ReplicatedStorage:FindFirstChild(plr.Name .. "_FMPM")
        if MPM and CM and MPMPMultiplier and FMPM then --So it doesn't error!
            FMPM.Value = (MPM.Value * MPMMultiplier.Value)
            CM.Value = (CM.Value + FMPM.Value)
        end
    end
    wait(1)
end
--LocalScript in the GUI:

local CMText = script:WaitForChild("MoneyGui").Frame.CurrentMoney
local CM
repeat
    CM = game.ReplicatedStorage:FindFirstChild(game.Players.LocalPlayer.Name .. "_CM")
    wait()
until CM

CMText.Text = CM.Value
CM.Changed:connect(function()
    CMText.Text = CM.Value
    print("Money changed!")
end)
0
It doesn't know what "plr" is on line 22-25, and considering this is a normal script.. unmiss 337 — 9y
0
My bad, line 21 should have been `for _, plr` not `for _, v` adark 5487 — 9y
Ad

Answer this question