Hi all,
I want to fix my issue regarding my new ClickPower variable which appears to always return nil. Recently I learned about ProfileService and watched a youtube tutorial on it. I followed the tutorial and created the variables with ProfileService correctly with no issues, later I created more variables and used them with ProfileService and experienced no issues.
Recently I decided that I wanted to create a ClickPower variable for players so I can edit how ‘powerful’ a players click is, however when I initialize it in this code:
local saveStructure = {
Clicks = 0, --Clicks for each player,
ClickPower = 1 --how much is a players click worth?
}
and try call it later with:
if profile.Data.ClickPower == nil then --this if block does not run(why though? It should!)
profile.Data.ClickPower = 1 --this never happens for some weird reason.
end
Clicks.Value = profile.Data.Clicks --this works just fine
ClickPower.Value = profile.Data.ClickPower --here I get a warning: 00:33:16.891 value of type nil cannot be converted to a number - Server - ProfileCacher:37, indicating that profile.Data.ClickPower is nil
ClickPower appears to be nil(according to errors and I tried to do math operations with ClickPower, however I keep on getting nil warnings/errors.
To clarify, “Clicks” variable works just fine, but “ClickPower” is nil.
I tried searching for solutions but was not able to find anything similar. I tried print testing and all I see is ClickPower = nil which is so bizarre(I literally initalized it) and I tried changing the name of the ClickPower variable to something else and to no avail, the newly named variable is nil. I am not advanced enough to read through the whole 2k or 3k lines of code that profileService has to understand where this error arises and im hoping someone else has experienced similar. If you know/want to know anything please feel free to tell/ask me!
I am unable to find the youtube tutorial I followed, so im going to paste all the code here(as a person experiencing this error I recommend looking through lines from 1- 47)
local Players = game:GetService('Players')
local cachedprofiles = {}
local ProfileService = require(script.ProfileService)
local saveStructure = {
Clicks = 0, --Clicks for each player,
ClickPower = 1 --how much is a players click worth?
}
local PlayerProfileStore = ProfileService.GetProfileStore("PlayerData", saveStructure)
local function PlayerDataLoaded(player)
local profile = cachedprofiles[player]
local leaderstatsFolder = Instance.new("Folder")
leaderstatsFolder.Name = "leaderstats"
leaderstatsFolder.Parent = player
local Clicks = Instance.new('IntValue')
Clicks.Name = "Clicks"
Clicks.Value = profile.Data.Clicks
Clicks.Parent = leaderstatsFolder
local ClickPower = Instance.new('IntValue')
ClickPower.Name = "ClickPower"
ClickPower.Value = profile.Data.ClickPower
ClickPower.Parent = leaderstatsFolder
task.spawn(function()
while true do
local profile = cachedprofiles[player]
if profile ~= nil then
if profile.Data.ClickPower == nil then
profile.Data.ClickPower = 1
end
Clicks.Value = profile.Data.Clicks
ClickPower.Value = profile.Data.ClickPower
else
break
end
task.wait(0.1)
end
end)
print(player.Name.." data is loaded!") --btw just letting yall know this message IS being printed even though im getting many warnings that ClickPower is nil!
end
local function playerAdded(player)
local profile = PlayerProfileStore:LoadProfileAsync("Player_"..player.UserId, "ForceLoad")
if profile ~= nil then
profile:ListenToRelease(function()
cachedprofiles[player] = nil
player:Kick("Your profile has been loaded remotely. Please rejoin.")
end)
if player:IsDescendantOf(Players) then
cachedprofiles[player] = profile
PlayerDataLoaded(player)
else
profile:Release()
end
else
player:Kick("Your saved data could not be loaded. Please rejoin.")
end
end
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
playerAdded(player)
end)
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = cachedprofiles[player]
if profile ~= nil then
profile:Release()
end
end)
return cachedprofiles