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

Sudden variable type shift?

Asked by 7 years ago
Edited 7 years ago

I'm new to coding in roblox, and I've just put a playerdata save script in my game. The problem I'm having is where external scripts are supposed to update stats, the variables are being passed as booleans. I was doing math and everything was working, then it suddenly stopped and said I couldn't do math anymore.

the module script:

local PlayerStatManager = {}

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

local AUTOSAVE_INTERVAL = 60

local sessionData = {}

--the offending function
--sudden shift to boolean for no reason
function PlayerStatManager:ChangeStat(player, statName, changeValue)
    sessionData[player][statName] = sessionData[player][statName] + changeValue
    print(sessionData[player][statName])
end

local function getPlayerData(player)
    return playerData:GetAsync(player.UserId)
end

local function savePlayerData(player)
    playerData:SetAsync(player.UserId, sessionData[player])
end

local function setupPlayerData(player)
    local data = getPlayerData(player)
    if not data then
        sessionData[player] = {Money = 0, Experience = 0}
        savePlayerData(player)
        print('new player')
    else
        sessionData[player] = data
        print('existing user')
        print(sessionData[player])
    end
end

local function autosave()
    while wait(AUTOSAVE_INTERVAL) do
        for player, data in pairs(sessionData) do
            savePlayerData(player)
        end
    end
end

game.Players.PlayerAdded:connect(setupPlayerData)

game.Players.PlayerRemoving:connect(function(player)
    savePlayerData(player)
    sessionData[player] = nil
end)

spawn(autosave)

return PlayerStatManager

the function in my update script:

game.Players.PlayerAdded:connect(function(player)
    while wait(2) do
        PlayerStatManager:ChangeStat(player, 'Money', 5)
        PlayerStatManager:ChangeStat(player, 'Experience', 5)
    end
end)

the new error I'm getting:

12:27:02.727 - ServerStorage.playerData_Module:11: attempt to perform arithmetic on field '?' (a boolean value)

****EDIT Upon loading these exact scripts into a new place they work as expected, I get no errors and the saved values are changing. I have narrowed it down to this specific place as being broken.

0
Can you include the full module script, also you are making an infinite loop for every player that joins the game. User#5423 17 — 7y
0
i know it's an infinite loop, i just needed to test that it was working, and i'll include the whole script syntaxjedi 22 — 7y
0
is this the module script in ServerStorage.playerData_Module ? User#5423 17 — 7y
0
yes, it still shows the player and prints out the userId, but I can't add to money or experience anymore syntaxjedi 22 — 7y
View all comments (4 more)
0
I've managed to narrow it down to "changeValue", when I change "sessionData[player][statName] = sessionData[player][statName] + changeValue" to "sessionData[player][statName] = sessionData[player][statName][changeValue]" I get "13:50:48.781 - ServerStorage.playerData_Module:11: attempt to index field '?' (a boolean value)" syntaxjedi 22 — 7y
0
I would recommend that you print(changeValue) to check that its not a boolean User#5423 17 — 7y
0
printing only changevalue outputs "nil nil" syntaxjedi 22 — 7y
0
correction: printing [changeValue] outputs nil because that isn't where the variable is stored, printing changeValue prints the correct value syntaxjedi 22 — 7y

Answer this question