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

How do I fix this issue with detecting IntValue changes?

Asked by 3 years ago
Edited 3 years ago

I am having an error when I try to detect IntValue changes for a player.

local coinStore = game:GetService("DataStoreService"):GetDataStore("coinsv1")
local jumpAmountStore = game:GetService("DataStoreService"):GetDataStore("jumpv1")
local levelStore = game:GetService("DataStoreService"):GetDataStore("levelv1")

game.Players.PlayerAdded:Connect(function(p)
    local leaderstats = Instance.new("Folder", p)
    local coins = Instance.new("IntValue", leaderstats)
    local levels = Instance.new("IntValue", leaderstats)
    local jumpAmount = Instance.new("IntValue", leaderstats)
    leaderstats.Name = "leaderstats"
    coins.Name = "Coins"
    levels.Name = "Level"
    jumpAmount.Name = "Jumps"
    coins.Value = 0
    levels.Value = 1
    jumpAmount.Value = 0

    local jumpPower
    p.CharacterAdded:Connect(function(c)
        jumpPower = c.Humanoid.JumpPower
        c.Humanoid.JumpPower = jumpPower
    end)

    local data
    local datatwo
    local datathree
    local success, errormessage = pcall(function()
        data = coinStore:GetAsync(p.UserId.."-coins")
        datatwo = jumpAmountStore:GetAsync(p.UserId.."-jumps")
        datathree = levelStore:GetAsync(p.UserId.."-level")
    end)

    if success then
        coins.Value = data
        levels.Value = datathree
        jumpAmount.Value = datatwo
    else
        print(errormessage)
    end

    local recentcoins
    jumpAmount.Changed:Connect(function()
        if jumpAmount.Value == datatwo + 20 then
            levels.Value = levels.Value + 1
            recentcoins = jumpAmount.Value
        elseif jumpAmount.Value == recentcoins + 20 then
            levels.Value = levels.Value + 1
            recentcoins = jumpAmount.Value  
        end
    end)

    local recentjumps
    jumpAmount.Changed:Connect(function()
        if jumpAmount.Value == datatwo + 10 then
            local jumps = jumpAmount.Value / 10
            jumps = jumps + 50
            local cjump = p.Character.Humanoid.JumpPower
            cjump = jumps
            recentjumps = jumpAmount.Value
        elseif jumpAmount.Value == recentjumps + 10 then
            local jumps = jumpAmount.Value / 10
            jumps = jumps + 50
            local cjump = p.Character.Humanoid.JumpPower
            cjump = jumps
            recentjumps = jumpAmount.Value  
        end
    end)
end)

The error is at line 46 and 60. The error is attempt to perform arithmetic (add) on nil and number.

1 answer

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

On line 41, you set recentcoins variable to nil . So on line 46 and 60, it tries to add 20 and 10 to a nil value, causing the error. Set recentcoins to 0 instead of just nil.

EDIT: It's also the case with line 52. Fix that too.

Ad

Answer this question