Sorry, forgot to change the category
I’m having a weird problem that I’m really confused about. Basically, I’m trying to use DataStore2 for my games, but it seems that whenever I set a value, then leave the game, this value goes back to 0 and it’s not in a table. In the code below, if I say
playerDataStore:Set({
["Coins"] = 100
})
, on the 25th line, then it will last for that session, but, when I leave the game and rejoin with that code still there, it goes back to 100, while deleting it makes it go back to 0.
The most important lines are the local function purchase()
, the CoinsPart clicked function (this is just as a test) at the bottom, and the update called with OnUpdate
--Editable variables
local NUMBER_OF_TYCOONS = 1
--Defining Variables
local Debris = game:GetService("Debris")
local DS2 = require(script.DataStore2)
local DSS = game:GetService("DataStoreService")
local gameInformationStore = DSS:GetDataStore("GameInformation")
--Spawning in all tycoons
for x = 1, NUMBER_OF_TYCOONS, 1 do
local tycoonClone = game.ServerStorage:WaitForChild("TycoonToClone"):Clone()
tycoonClone.Name = "Tycoon" .. x
tycoonClone.Parent = game.Workspace.Tycoons
end
game.Players.PlayerAdded:Connect(function(player)
local playerDataStore = DS2("playerData",player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Parent = leaderstats
--playerDataStore:Set({
-- ["Coins"] = 100
--})
local function update(updatedValue)
print("Value updated!")
print(playerDataStore)
print(updatedValue)
--if playerDataStore:Get() == nil then
--print("Was nil")
--end
coins.Value = playerDataStore:Get()["Coins"]
end
update()
playerDataStore:OnUpdate(update)
coins.Parent = leaderstats
local playerGui = player.PlayerGui
local settingsGui = playerGui:WaitForChild("SettingsGui")
local settingsFrame = settingsGui:WaitForChild("SettingsFrame")
local gameInformation = settingsFrame:WaitForChild("GameInformation")
local serverVersion = gameInformation.ServerVersion
local latestGameVersion = gameInformation.LatestGameVersion
local gameVersion = gameInformationStore:GetAsync("Version")
print(gameVersion)
serverVersion.Text = "Game Version: " .. gameVersion
latestGameVersion.Text = "Latest Version: " .. gameVersion
local function checkForNewVersion()
while true do
local success, err = pcall(function()
latestGameVersion.Text = "Latest Version: " .. gameInformationStore:GetAsync("Version")
end)
wait(6)
end
end
spawn(checkForNewVersion)
end)
--Making killbricks work
for _, descendant in pairs(game.Workspace:GetDescendants()) do
if descendant.Name == "Killbrick" then
if descendant:FindFirstChild("Hitbox") then
descendant.Hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if descendant.Hitbox:FindFirstChild("DamageNumber") then
hit.Parent.Humanoid.Health -= descendant.Hitbox.DamageNumber.Value
else
hit.Parent.Humanoid.Health = 0
end
end
end)
else
descendant.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if descendant:FindFirstChild("DamageNumber") then
hit.Parent.Humanoid.Health -= descendant.DamageNumber.Value
else
hit.Parent.Humanoid.Health = 0
end
end
end)
end
--Make GoalParts work
elseif descendant.Name == "GoalPart" then
local debounce = false
descendant.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and (game.Players:GetPlayerFromCharacter(hit.Parent).UserId == descendant.Parent.Parent.Parent.TycoonInformation.OwnerId.Value or (hit.Parent:FindFirstChild("NPCOwnerId") and hit.Parent.NPCOwnerId == descendant.Parent.Parent.Parent.TycoonInformation.OwnerId.Value)) then
if debounce == false then
local particleEmitter = Instance.new("ParticleEmitter")
particleEmitter.Color = ColorSequence.new{ColorSequenceKeypoint.new(0,Color3.fromRGB(255, 255, 0)),ColorSequenceKeypoint.new(1,Color3.fromRGB(255, 255, 0))}
particleEmitter.Acceleration = Vector3.new(0,10,0)
particleEmitter.Parent = descendant
Debris:AddItem(particleEmitter,5)
game.Players:GetPlayerFromCharacter(hit.Parent).leaderstats.Coins.Value += descendant.Reward.Value
hit.Parent.HumanoidRootPart.Position = descendant.Parent.StartPart.Position + Vector3.new(0,2,0)
debounce = true
wait(2.5)
debounce = false
end
end
end)
-- Code for buttons
elseif descendant.Name == "Buttons" then
for _, button in pairs (descendant:GetChildren()) do
if button:IsA("Part") and string.find(button.Name,"Obby") then -- If it is a button
local debounce = false
button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
local playerThatTouched = game.Players:GetPlayerFromCharacter(hit.Parent)
if playerThatTouched.UserId == button.Parent.Parent.TycoonInformation.OwnerId.Value then -- if the player who touched is the owner
debounce = true
local function purchase(cost,button,hit)
local playerData = DS2("playerData",game.Players:GetPlayerFromCharacter(hit.Parent))
if playerThatTouched.leaderstats.Coins.Value >= cost then
print(playerData)
local dataClone = playerData:Get()
dataClone["Coins"] -= cost
print(dataClone)
playerData:Set(dataClone)
print(playerData)
for _, part in pairs(button.Parent.Parent.Obbies[button.Name]:GetChildren()) do
if part:IsA("Part") or part:IsA("TrussPart") or part:IsA("MeshPart") or part:IsA("UnionOperation") then
if part.Name ~= "StartPart" then
part.CanCollide = true
part.Transparency = 0
part.CanTouch = true
for _, texture in pairs(part:GetChildren()) do
if texture:IsA("Texture") then
texture.Transparency = 0
end
end
end
end
end
--Makes all buttons that should be revealed after a button is pressed be revealed
for _, buttonToSearch in pairs(descendant:GetChildren()) do
if buttonToSearch:IsA("Part") then
if buttonToSearch.Dependant.Value == button then
buttonToSearch.Transparency = 0
buttonToSearch.CanCollide = true
buttonToSearch.BillboardGui.Enabled = true
buttonToSearch.CanTouch = true
end
end
end
button:Destroy()
end
wait(1)
debounce = false
end
if button.BillboardGui.Cost.Text == "FREE" then
purchase(0,button,hit)
else
local costString = string.gsub(button.BillboardGui.Cost.Text,"%$","")
local cost = tonumber(costString)
purchase(cost,button,hit)
end
end
end
end)
end
end
-- Make going into tycoon make player owner, if there is not yet an owner
elseif descendant.Name == "TouchBox" then
if string.find(descendant.Parent.Parent.Name,"Tycoon") then
descendant.Touched:Connect(function(hit)
local playerThatTouched = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and playerThatTouched then
local ownerId = descendant.Parent.Parent.TycoonInformation.OwnerId
if ownerId ~= 0 then
ownerId.Value = playerThatTouched.UserId
end
end
end)
end
end
end
-- Make coins part give coins
game.Workspace:WaitForChild("CoinsPart").ClickDetector.MouseClick:Connect(function(player)
local playerDataStore = DS2("playerData",player)
local newData = playerDataStore:Get()
print(newData)
newData["Coins"] += 50
playerDataStore:Set(newData)
end)
And here’s a copy of the game, missing some game parts, but everything you need to test
scripthelp.rbxl (54.9 KB)
Any ideas for why it’s doing this?