Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i solve this loading from datastore issue?

Asked by 1 year ago

i have a multipliers shop that basically multiplies the amount of x the player earns when they collect an item ( coins, etc) but when it saves the data it saves to the player properly, however the loading portion is the messed up part. It loads into rep storage and for the first player thats great however when theres more than 1 player it messes up and replicates the first player's multipliers to everyone else. Should i save a value to a table and then set it to the bool value? where would that bool value end up? in rep storage, server storage, or the player themselves? i have alot of unknowns when making this script, i wanted to tackle a harder script for myself but i think i bit off more than i can chew

local replicatedstorage = game:GetService("ReplicatedStorage")
local remotecoins = replicatedstorage.Multipliers.CoinsMultiplier
local remotecoils = replicatedstorage.Multipliers.CoilsMultiplier
local remotepetluck = replicatedstorage.Multipliers.PetLuckMultiplier
local datastore = game:GetService("DataStoreService")
local multipliers = datastore:GetDataStore("Multipliers")

game.Players.PlayerAdded:Connect(function(player)
    local data1
    local data2
    local data3
    local data4
    local data5
    local data6

    local success, errorMessage = pcall(function()

        data1 = multipliers:GetAsync(player.UserId .. "-Springsx")
        data2 = multipliers:GetAsync(player.UserId .. "-Coinsx")
        data3 = multipliers:GetAsync(player.UserId .. "-PetLuckx")
        data4 = multipliers:GetAsync(player.UserId .. "-SpringsCost")
        data5 = multipliers:GetAsync(player.UserId .. "-CoinsCost")
        data6 = multipliers:GetAsync(player.UserId .. "-PetLuckCost")
        print(data1)
        print(data2)
        print(data3)
        print(data4)
        print(data5)
        print(data6)
    end)

    if success then
        print("Success, loaded multipliers")
        print(data1)
        print(data2)
        print(data3)
        print(data4)
        print(data5)
        print(data6)

        if data1 and data4 then
            remotecoils.Multiplier.Value = data1
            remotecoils.MultiplierCost.Value = data4

        else
            remotecoils.Multiplier.Value = "1"
            remotecoils.MultiplierCost.Value = "100"
        end
        if data2 and data5 then
            remotecoins.Multiplier.Value = data2
            remotecoins.MultiplierCost.Value = data5
        else
            remotecoins.Multiplier.Value = "1"
            remotecoins.MultiplierCost.Value = "100"
        end
        if data3 and data6 then
            remotepetluck.Multiplier.Value = data3
            remotepetluck.MultiplierCost.Value = data6
        else
            remotepetluck.Multiplier.Value = "1"
            remotepetluck.MultiplierCost.Value = "1000"
        end
    else
        print(errorMessage)

        remotecoils.Multiplier.Value = "1"
        remotecoils.MultiplierCost.Value = "100"

        remotecoins.Multiplier.Value = "1"
        remotecoins.MultiplierCost.Value = "100"

        remotepetluck.Multiplier.Value = "1"
        remotepetluck.MultiplierCost.Value = "1000"

    end
end)


game.Players.PlayerRemoving:Connect(function(player)

    local coilsx = remotecoils.Multiplier.Value
    local coinsx = remotecoins.Multiplier.Value
    local petluckx = remotepetluck.Multiplier.Value
    local coilsc = remotecoils.MultiplierCost.Value
    local coinsc = remotecoins.MultiplierCost.Value
    local petluckc = remotepetluck.MultiplierCost.value

    local success, errorMessage = pcall(function()
        multipliers:SetAsync(player.UserId .. "-Springsx", coilsx)
        multipliers:SetAsync(player.UserId .. "-Coinsx", coinsx)
        multipliers:SetAsync(player.UserId .. "-PetLuckx", petluckx)
        multipliers:SetAsync(player.UserId .. "-SpringsCost", coilsc)
        multipliers:SetAsync(player.UserId .. "-CoinsCost", coinsc)
        multipliers:SetAsync(player.UserId .. "-PetLuckCost", petluckc)
    end)

    if success then
        print("Saved Data Successfully")
    else
        print(errorMessage)
    end

end)

game:BindToClose(function()
    wait(7)
end)

any help would be great, im just hoping that the problem isnt too great and its something small like what i mention in terms of it being just replacing what the value is saved to and where.

0
You may have to store your values into a table value like ``local data = []`` Xx_ashcarter13 17 — 1y

1 answer

Log in to vote
1
Answered by
0x5y 105
1 year ago
Edited 1 year ago

Okay okay, where to start where to start...

First and foremost you should be using one datastore, and one key for each user. Secondly, you are not separating out each player when you save/load their stats.

I have rewritten your script to suit the above.

Please bear in mind, this code is 100% untested so modification may be necessary.


local datastore = game:GetService("DataStoreService") local mutliplierDatastore = datastore:GetDataStore("Multipliers") game.Players.PlayerAdded:Connect(function(player) local playerData local success, errorMessage = pcall(function() playerData = multipliers:GetAsync(tostring(player.UserId)) print(playerData) end) local CoilsMultiplier = Instance.new("IntValue"); CoilsMultiplier.Name = "CoilsMultiplier"; CoilsMultiplier.Parent = player; local CoinsMultiplier = Instance.new("IntValue"); CoinsMultiplier.Name = "CoinsMultiplier"; CoinsMultiplier.Parent = player; local PetLuckMultiplier = Instance.new("IntValue"); PetLuckMultiplier.Name = "PetLuckMultiplier"; PetLuckMultiplier.Parent = player; local CoilsCost = Instance.new("IntValue"); CoilsCost.Name = "CoilsCost"; CoilsCost.Parent = player; local CoinsCost = Instance.new("IntValue"); CoinsCost.Name = "CoinsCost"; CoinsCost.Parent = player; local PetLuckCost = Instance.new("IntValue"); PetLuckCost.Name = "PetLuckCost"; PetLuckCost.Parent = player; if success and playerData ~= nil then print("Success, loaded multipliers") print(playerData) CoilsMultiplier.Value = playerData["CoilsMultiplier"] or 1 CoinsMultiplier.Value = playerData["CoinsMultiplier"] or 1 PetLuckMultiplier.Value = playerData["PetLuckMultiplier"] or 1 CoilsCost.Value = playerData["CoilsCost"] or 100 CoinsCost.Value = playerData["CoinsCost"] or 100 PetLuckCost.Value = playerData["PetLuckCost"] or 1000 else print(errorMessage) CoilsMultiplier.Value = 1 CoinsMultiplier.Value = 1 PetLuckMultiplier.Value = 1 CoilsCost.Value = 100 CoinsCost.Value = 100 PetLuckCost.Value = 1000 end end) game.Players.PlayerRemoving:Connect(function(player) local playerData = {} playerData["CoilsMultiplier"] = player.CoilsMultiplier.Value playerData["CoinsMultiplier"] = player.CoinsMultiplier.Value playerData["PetLuckMultiplier"] = player.PetLuckMultiplier.Value playerData["CoilsCost"] = player.CoilsCost.Value playerData["CoinsCost"] = player.CoinsCost.Value playerData["PetLuckCost"] = player.PetLuckCost.Value local success, errorMessage = pcall(function() mutliplierDatastore:SetAsync(tostring(player.UserId), playerData) end) if success then print("Saved Data Successfully") else print(errorMessage) end end) game:BindToClose(function() wait(7) end)
0
im having an error that i cant seem to figure out im getting the error (ServerScriptService.MultipliersHandler:40: attempt to index nil with 'CoilsMultiplier') it breaks like that the first time, if i load in again before wiping the datastore info, it loads me in with 0 multipliers. De_Bosnian 7 — 1y
0
For some reason, `playerData` is nil. When it gets printed on line 9 and line 38, what does it print? The error is likely due to something on my part, where I did not check if `playerData` actually exists, causing your error. I have edited my post to check for that. Let me know if it works. 0x5y 105 — 1y
Ad

Answer this question