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

[SOLVED] I have a very big bug breaking issue. Can only find the solution to this error?

Asked by
Cowgato 33
2 years ago
Edited by Xapelize 2 years ago

This question has been solved by the original poster.

The scripts that I have will be pretty long so bear with me (however, I will show you which section of the script is mainly important to this issue)

I am trying to make a data storage system in my module script. What I am trying to attempt is saving the players money, however, if I use a certain script, the game can't recognize the value of the money and I get an error message saying this: ServerScriptService.Components.Button:35: attempt to compare number <= nil (this error message applies to another module script btw)

The script below works fine for other things except for the money system: (look near the bottom of script)

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function LeaderboardSetup(value)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = value
    money.Parent = leaderstats
    return leaderstats
end

local function LoadData(player)
    local success, result = pcall(function()
        return PlayerData:GetAsync(player.UserId)
    end)
    if not success then
        warn(result)
    end
    return success, result
end

local function SaveData(player, data)
    local success, result = pcall(function()
        PlayerData:SetAsync(player.UserId, data)
    end)
    if not success then
        warn(result)
    end
    return success
end

local sessionData = {}

local playerAdded = Instance.new("BindableEvent")
local playerRemoving = Instance.new("BindableEvent")

local PlayerManager = {}

PlayerManager.PlayerAdded = playerAdded.Event
PlayerManager.PlayerRemoving = playerRemoving.Event

function PlayerManager.Start()
    for _, player in ipairs(Players:GetPlayers()) do
        coroutine.wrap(PlayerManager.OnPlayerAdded)(player)
    end

    Players.PlayerAdded:Connect(PlayerManager.OnPlayerAdded)
    Players.PlayerRemoving:Connect(PlayerManager.OnPlayerRemoving)

    game:BindToClose(PlayerManager.OnClose)
end

function PlayerManager.OnPlayerAdded(player)
    player.CharacterAdded:Connect(function(character)
        PlayerManager.OnCharacterAdded(player, character)
    end)

    local success, data = LoadData(player)
    sessionData[player.UserId] = success and data or {
        Money = 0,
        UnlockIds = {}
    }

    local leaderstats = LeaderboardSetup(PlayerManager.GetMoney(player))
    leaderstats.Parent = player

    playerAdded:Fire(player)
end

function PlayerManager.OnCharacterAdded(player, character)
    local humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Died:Connect(function()
            wait(3)
            player:LoadCharacter()
        end)
    end
end

function PlayerManager.GetMoney(player)
--vv HERES THE SCRIPT BEFORE THE CHANGE vv--
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local money = leaderstats:FindFirstChild("Money")
        if money then
            return money.Value
        end
    end 
    return 0
--^^ HERES THE SCRIPT BEFORE THE CHANGE ^^--
end

function PlayerManager.SetMoney(player, value)
    if value then
        sessionData[player.UserId].Money = value

        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            local money = leaderstats:FindFirstChild("Money")
            if money then
                money.Value = value
            end
        end     
    end
end

function PlayerManager.OnPlayerRemoving(player)
    SaveData(player, sessionData[player.UserId])
    playerRemoving:Fire(player)
end

function PlayerManager.OnClose()
    for _, player in ipairs(Players:GetPlayers()) do
        PlayerManager.OnPlayerAdded(player)
    end
end

return PlayerManager

However, the script below breaks the money system and can't recognize it: (look near the bottom of script again)

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function LeaderboardSetup(value)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = value
    money.Parent = leaderstats
    return leaderstats
end

local function LoadData(player)
    local success, result = pcall(function()
        return PlayerData:GetAsync(player.UserId)
    end)
    if not success then
        warn(result)
    end
    return success, result
end

local function SaveData(player, data)
    local success, result = pcall(function()
        PlayerData:SetAsync(player.UserId, data)
    end)
    if not success then
        warn(result)
    end
    return success
end

local sessionData = {}

local playerAdded = Instance.new("BindableEvent")
local playerRemoving = Instance.new("BindableEvent")

local PlayerManager = {}

PlayerManager.PlayerAdded = playerAdded.Event
PlayerManager.PlayerRemoving = playerRemoving.Event

function PlayerManager.Start()
    for _, player in ipairs(Players:GetPlayers()) do
        coroutine.wrap(PlayerManager.OnPlayerAdded)(player)
    end

    Players.PlayerAdded:Connect(PlayerManager.OnPlayerAdded)
    Players.PlayerRemoving:Connect(PlayerManager.OnPlayerRemoving)

    game:BindToClose(PlayerManager.OnClose)
end

function PlayerManager.OnPlayerAdded(player)
    player.CharacterAdded:Connect(function(character)
        PlayerManager.OnCharacterAdded(player, character)
    end)

    local success, data = LoadData(player)
    sessionData[player.UserId] = success and data or {
        Money = 0,
        UnlockIds = {}
    }

    local leaderstats = LeaderboardSetup(PlayerManager.GetMoney(player))
    leaderstats.Parent = player

    playerAdded:Fire(player)
end

function PlayerManager.OnCharacterAdded(player, character)
    local humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Died:Connect(function()
            wait(3)
            player:LoadCharacter()
        end)
    end
end

function PlayerManager.GetMoney(player)
--vv HERES THE SCRIPT THAT CHANGES AND BREAKS THE MONEY SYSTEM vv--
      return sessionData[player.UserId].Money
--^^HERES THE SCRIPT THAT CHANGES AND BREAKS THE MONEY SYSTEM^^--
end

function PlayerManager.SetMoney(player, value)
    if value then
        sessionData[player.UserId].Money = value

        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            local money = leaderstats:FindFirstChild("Money")
            if money then
                money.Value = value
            end
        end     
    end
end

function PlayerManager.OnPlayerRemoving(player)
    SaveData(player, sessionData[player.UserId])
    playerRemoving:Fire(player)
end

function PlayerManager.OnClose()
    for _, player in ipairs(Players:GetPlayers()) do
        PlayerManager.OnPlayerAdded(player)
    end
end

return PlayerManager

I'm really sorry if this is hard to follow through without much context, but any help would be extremely appreciated!

1 answer

Log in to vote
0
Answered by
Cowgato 33
2 years ago

Sorry guys, I've figured it out. Just decided to work for some reason haha

Ad

Answer this question