Datastore works in Roblox Studio but not in-game

372 views Asked by At

My problem is my data store. I have been working on a game for about a month and I though it would be nice to add a currency system, well with a currency system I realized I was going to need a way to save the currency and items the players bought. So, I read up on data stores, tables all that stuff. after a little while I came out with a functioning database.

It works without a flaw in the studio (obviously with API ###vices enabled). But it does not seem to register when I play the game from the website. Here is the script I am using

local HttpService = game:GetService("HttpService")
local DataStore = game:GetService("DataStoreService")
local data1 = DataStore:GetDataStore("PawsDataStore")
local data2 = DataStore:GetDataStore("PawsDataStore2")
local playerdata = {}
local OwnedChampions = {}

game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local rank = Instance.new("IntValue", leaderstats)
rank.Name = "Rank"

local shards = Instance.new("IntValue", leaderstats)
shards.Name = "Shards"

local champions = Instance.new("Folder", player)
champions.Name = "OwnedChampions"

local abyssal = Instance.new("BoolValue", champions)
abyssal.Name = "Abyssal"
local biomage = Instance.new("BoolValue", champions)
biomage.Name = "Biomage"
local permafrost = Instance.new("BoolValue", champions)
permafrost.Name = "Permafrost"

local holder = Instance.new("BoolValue", champions)
holder.Name = " "

if not data1:GetAsync(player.UserId, playerdata) then
    print("Making new data entry for ".. player.Name)
    playerdata[1] = 1
    playerdata[2] = 500
    
    OwnedChampions[1] = false
    OwnedChampions[2] = false
    OwnedChampions[3] = false
    
    local encoding = HttpService:JSONEncode(playerdata)
    local encoding2 = HttpService:JSONEncode(OwnedChampions)
    
    data1:SetAsync(player.UserId, encoding)
    data2:SetAsync(player.UserId, encoding2)
    
    player.leaderstats.Rank.Value = playerdata[1]
    player.leaderstats.Shards.Value = playerdata[2]
    
    player.OwnedChampions.Abyssal.Value = OwnedChampions[1]
    player.OwnedChampions.Biomage.Value = OwnedChampions[2]
    player.OwnedChampions.Permafrost.Value = OwnedChampions[3]
    
    player.leaderstats.Rank.Changed:connect(function()
        playerdata[1] = player.leaderstats.Rank.Value
        local encoding = HttpService:JSONEncode(playerdata)
        data1:SetAsync(player.UserId, encoding)
    end)
    
    player.leaderstats.Shards.Changed:connect(function()
        playerdata[2] = player.leaderstats.Shards.Value
        local encoding = HttpService:JSONEncode(playerdata)
        data1:SetAsync(player.UserId, encoding)
    end)
    
    player.OwnedChampions.Abyssal.Changed:connect(function()
        OwnedChampions[1] = player.OwnedChampions.Abyssal.Value
        local encoding2 = HttpService:JSONEncode(OwnedChampions)
        data2:SetAsync(player.UserId, encoding2)
    end)
    
    player.OwnedChampions.Biomage.Changed:connect(function()
        OwnedChampions[2] = player.OwnedChampions.Biomage.Value
        local encoding2 = HttpService:JSONEncode(OwnedChampions)
        data2:SetAsync(player.UserId, encoding2)
    end)
    
    player.OwnedChampions.Permafrost.Changed:connect(function()
        OwnedChampions[3] = player.OwnedChampions.Permafrost.Value
        local encoding2 = HttpService:JSONEncode(OwnedChampions)
        data2:SetAsync(player.UserId, encoding2)
    end)
else
    local encodedvalues  = data1:GetAsync(player.UserId)
    local decoding = HttpService:JSONDecode(encodedvalues)
    
    playerdata = decoding
    
    rank.Value = decoding[1]
    shards.Value = decoding[2]
    
    local encodedvalues2  = data2:GetAsync(player.UserId)
    local decoding2 = HttpService:JSONDecode(encodedvalues2)
    
    OwnedChampions = decoding2
    
    abyssal.Value = decoding2[1]
    biomage.Value = decoding2[2]
    permafrost.Value = decoding2[3]
    
    player.leaderstats.Rank.Changed:connect(function()
        playerdata[1] = player.leaderstats.Rank.Value
        local encoding = HttpService:JSONEncode(playerdata)
        data1:SetAsync(player.UserId, encoding)
    end)
    
    player.leaderstats.Shards.Changed:connect(function()
        playerdata[2] = player.leaderstats.Shards.Value
        local encoding = HttpService:JSONEncode(playerdata)
        data1:SetAsync(player.UserId, encoding)
    end)
    
    player.OwnedChampions.Abyssal.Changed:connect(function()
        OwnedChampions[1] = player.OwnedChampions.Abyssal.Value
        local encoding2 = HttpService:JSONEncode(OwnedChampions)
        data2:SetAsync(player.UserId, encoding2)
    end)
    
    player.OwnedChampions.Biomage.Changed:connect(function()
        OwnedChampions[2] = player.OwnedChampions.Biomage.Value
        local encoding2 = HttpService:JSONEncode(OwnedChampions)
        data2:SetAsync(player.UserId, encoding2)
    end)
    
    player.OwnedChampions.Permafrost.Changed:connect(function()
        OwnedChampions[3] = player.OwnedChampions.Permafrost.Value
        local encoding2 = HttpService:JSONEncode(OwnedChampions)
        data2:SetAsync(player.UserId, encoding2)
    end)
end
end)

game.Players.PlayerRemoving:connect(function(player)
local encoding = HttpService:JSONEncode(playerdata)
local encoding2 = HttpService:JSONEncode(OwnedChampions)
data1:SetAsync(player.UserId, encoding)
data2:SetAsync(player.UserId, encoding2)
end)

It is a normal script placed in server script service.I also thought it might help that I note that when I begin the game on the website it loads the data fine but it seems to have trouble writing to the data store. Data I create on the studio gets saved to the datastore and functions properly.

0

There are 0 answers