This script is supposed to save the amount of Displays (currency) that the player has after they left the game. There are 3 scripts, but the ServerScriptService one is the one that causes the problem.
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("StoreKey")
local playerData = script.PlayerData
local function SaveData(player)
local data = {}
for _, folder in pairs(player:GetChildren()) do
if playerData:FindFirstChild(folder.Name) then
if playerData[folder.Name]:GetAttribute("SaveChildren") == true then
data[folder.Name] = {}
if playerData[folder.Name]:GetAttribute("SaveChildrenValues") == true then
for _, child in pairs(folder:GetChildren()) do
if not child:GetAttribute("DonNotSaveValue") then
table.insert(data[folder.Name], {child.Name, child.Value, child.ClassName})
end
end
else
for _, child in pairs(folder:GetChildren()) do
if not child:GetAttribute("DoNotSaveValue") then
table.insert(data[folder.Name], {child.Name, child.ClassName})
end
end
end
end
end
end
local succes, errorMsg = pcall(function()
dataStore:SetAsync(player.UserId.."Key", data)
end)
if succes then
print("Data Saved")
else
warn(errorMsg)
end
end
local function LoadData(player)
for _, v in pairs(playerData:GetChildren()) do
v:Clone().Parent = player
end
local data
local succes, errorMsg = pcall(function()
data = dataStore:GetAsync(player.UserId.."Key")
end)
if errorMsg then --Save feature for error join
warn(errorMsg)
player:Kick("Could not load your data please try again")
end
if data then --checking how much data there is.
for i,v in pairs(data) do
if #v > 0 then
for x, c in pairs(v) do
if player[tostring(i)]:FindFirstChild(c[1]) then
player[tostring(i)]:FindFirstChild(c[1]).Value = c[2]
else
local value
if c[3] == nil then
value = Instance.new(tostring(c[2]))
else
value = Instance.new(tostring(c[3]))
end
value.Name = c[1]
value.Value = c[2]
value.Parent = player[tostring(i)]
end
end
end
end
end
end
local function PlayerRemoving(player)
SaveData(player)
end
players.PlayerAdded:Connect(LoadData)
players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(function()
for _, player in pairs(players:GetPlayers()) do
coroutine.wrap(SaveData)(player)
end
end)
There’s not much else to say. The script for when the player collects the currency is here. It is located in the Prompt which is in the currency’s model.
local Prompt = script.Parent
local Att = Prompt.Parent
local Center = Att.Parent
local Display = Center.Parent
local Sound = Prompt.CollectSound
function onPromptTriggered(Player)
local PlayerGUI = Player.PlayerGui
local Values = PlayerGUI:WaitForChild("Values")
local Displays = Values.Displays
local DisplayAmount = Displays.DisplayAmount
Sound:Play()
Display:Destroy()
DisplayAmount.Value = DisplayAmount.Value + 1
end
Prompt.Triggered:Connect(onPromptTriggered)