Hello, could someone help me fix this issue in my script?
-
What do you want to achieve? This chunk of code is part of a larger script that creates leaderstats and loads/saves data. As turning strings to booleans is not a built-in function, I made (borrowed lol) a function that turns strings into booleans (tobool()). I want to run this in my function that unpacks the save data in a datastore (a large string)
-
What is the issue? tobool() is not running properly in the function. However, when it was used outside of the function, it worked properly. This is only erroring when the function actually runs (line 111), so the functions are likely being defined properly.
-
What solutions have you tried so far? I have tried using tobool(v) instead of tobool() as the 4th parameter, but it still returns a nil value.
tobool() function:
function tobool(str)
if str == "true" or str == "false" or str == "nil" then
local boolNew
if str == "true" then
boolNew = true
elseif str == "false" then
boolNew = false
elseif str == "nil" then
boolNew = nil
end
return boolNew
else
warn("Parameter must be a string!")
return
end
end
unpacksave() function:
function unpacksave(target_datastore, target_folder, target_table, function_type)
local packedstring = target_datastore:GetAsync(userId)
print(packedstring)
unpacked_table = string.split(packedstring, ",")
for i, v in unpacked_table do
print(tostring(i), tostring(v), typeof(v))
print(tostring(player:WaitForChild(tostring(target_folder.Name)[target_table[i]].Value))
function_type(v)
player:WaitForChild(tostring(target_folder.Name))[target_table[i]].Value = function_type(v)
end
end
Where unpacksave() actually runs:
unpacksave(tools_store, toolfolder, tools, tobool())
Entire script:
local players = game.Players
local datastoreserv = game:GetService("DataStoreService")
local coins_store = datastoreserv:GetDataStore("Coins")
local tools_store = datastoreserv:GetDataStore("Tools")
local tempstats_store = datastoreserv:GetDataStore("TemporaryStats")
--Player-independent functions
function tobool(str)
if str == "true" or str == "false" or str == "nil" then
local boolNew
if str == "true" then
boolNew = true
elseif str == "false" then
boolNew = false
elseif str == "nil" then
boolNew = nil
end
return boolNew
else
warn("Parameter must be a string!")
return
end
end
players.PlayerAdded:Connect(function(player)
userId = player.UserId
--Player-dependent functions
local tosavevalue = ""
function packsave(folder_to_search, datastore_to_search)
for i, v in folder_to_search do
if i <= 1 then
tosavevalue = tosavevalue ..tostring(v.Value)
else
tosavevalue = tosavevalue.. "," ..tostring(v.Value)
end
datastore_to_search:SetAsync(userId, tosavevalue)
print(tosavevalue, tostring(v.Value))
end
end
function unpacksave(target_datastore, target_folder, target_table, function_type)
local packedstring = target_datastore:GetAsync(userId)
print(packedstring)
unpacked_table = string.split(packedstring, ",")
for i, v in unpacked_table do
print(tostring(i), tostring(v), typeof(v))
print(tostring(player:WaitForChild(tostring(target_folder.Name))[target_table[i]].Value))
function_type(v)
player:WaitForChild(tostring(target_folder.Name))[target_table[i]].Value = function_type(v)
end
end
function instanceload()
print("aa")
end
--Setting leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
--Setting hidden values (folders)
toolfolder = Instance.new("Folder")
toolfolder.Name = "ToolsFolder"
toolfolder.Parent = player
tempstatfolder = Instance.new("Folder")
tempstatfolder.Name = "TempStats"
tempstatfolder.Parent = player
--Setting hidden values (tables)
--NOTE: tools_owned and similar tables are only used in pre-data loading (instance generation)
tools = {"rustybucket", "woodenbucket"}
local tools_owned = {false, false}
tempstats = {"capacity", "max_capacity"}
local tempstat_values = {0, 10}
--Setting hidden values (instances) (Nothing for now :( )
--Creating the tools owned values
for i, v in tools_owned do
local newInstance = Instance.new("BoolValue")
newInstance.Name = tools[i]
newInstance.Value = v
newInstance.Parent = toolfolder
end
--Loading data
wait(3)
local savedcoins = coins_store:GetAsync(userId)
print(tostring(savedcoins))
coins.Value = savedcoins
unpacksave(tools_store, toolfolder, tools, tobool())
end)
players.PlayerRemoving:Connect(function()
local setSuccess, errorMessage = pcall(function()
coins_store:SetAsync(userId, coins.Value)
--Saving multivalues with string to unpack
packsave(toolfolder:GetChildren(), tools_store)
end)
if not setSuccess then
errorMessage = "Data not saved!"
warn(errorMessage)
end
end)
(btw sry if this code is really messy, I haven’t tried to optimise it)
Thanks for reading this post!