#Why doesn't this datastoring work?

78 messages · Page 1 of 1 (latest)

crisp thunder
#

Would be nice if the solution was explained.

solid kettle
#

solution:

keen cedar
#

Omg.... You opened a function and never gave a fuck about it...

crisp thunder
austere elkBOT
#

studio** You are now Level 9! **studio

keen cedar
#

If we are seeing the entire bottom script then you should do:

game:BintToClose(function()
  for _,v in pairs(game.Players:GetChildren()) do
    savedata(v)
  end 
end)

savedata is a function you should make (just copy and paste everything you got under game.Players.PlayerRemoving till the first end of the 2nd if)

shrewd osprey
#

mfw non-modular programming 😠

#

make a datastore save function, call it on every player on close, call it on players leaving, save yourself writing the same shit 3 times on different event listeners

keen cedar
#

Yea... But you still have some end-s there that should not be there

crisp thunder
#

Is that so? Script analisys doesn't tell me anything

#

And when I try to delete some of those, error pops up (in the script analysis)

keen cedar
#

Then your script is nesting sooo much

#

And I can't tell if you need these end by seing the screenshot of your script

#

Maybe send the script as a .lua file?

crisp thunder
#
local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")
    
    
    
    local success, currentExperience = pcall(function()
        return experienceStore:GetAsync(player.UserId.."coins")
        end)
        if success then
        coins.Value = currentExperience
        print(currentExperience)
        end
end)
local function savedata()
    game.Players.PlayerRemoving:Connect(function(player)
        local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
        local success, errorMessage = pcall(function()
            experienceStore:SetAsync(player.UserId.."coins", coins.Value)

        end)

        local success2, errorMessage2 = pcall(function()
            experienceStore:UpdateAsync(player.UserId.."coins", coins.Value)
        end)
        if not success2 then
            error(errorMessage2)
        end
        if not success then
            error(errorMessage)

        end

game:BintToClose(function()
    for _,v in pairs(game.Players:GetChildren()) do
                savedata(v)
            end
        end)
    end)
    end
keen cedar
#

Dude... Copy and re-paste the code in your script

#

This is so hard to understand

crisp thunder
keen cedar
#

Omg...

#

The one you already have

#

Just copy and paste it back

#

IN YOUR SCRIPT

#

And then paste it here

#

like wtf... how should I understand where the function ends? Should I start counting the end?

crisp thunder
#

I pasted it here, it is my code, from the serverScriptsservice

keen cedar
#

Jeez man

#

Read what I said again

crisp thunder
#

Copy the code from the message, paste it into my script, and then paste it here

keen cedar
#

Dude

crisp thunder
#

So, you want me to copy YOUR code, paste it into my script, and paste the full script here?

keen cedar
#

Open the script where you have the code
Press CTRL+A then CTRL+C then CTRL+V
Now, you can copy again the script
CTRL+A then CTRL+C
And paste it there
CTRL+V using format

crisp thunder
#

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local coins = leaderstats:WaitForChild("Coins")

local success, currentExperience = pcall(function()
    return experienceStore:GetAsync(player.UserId.."coins")
    end)
    if success then
    coins.Value = currentExperience
    print(currentExperience)
    end

end)
local function savedata()
game.Players.PlayerRemoving:Connect(function(player)
local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
local success, errorMessage = pcall(function()
experienceStore:SetAsync(player.UserId.."coins", coins.Value)

    end)

    local success2, errorMessage2 = pcall(function()
        experienceStore:UpdateAsync(player.UserId.."coins", coins.Value)
    end)
    if not success2 then
        error(errorMessage2)
    end
    if not success then
        error(errorMessage)

    end

game:BintToClose(function()
for _,v in pairs(game.Players:GetChildren()) do
savedata(v)
end
end)
end)
end

keen cedar
#

Now use format

crisp thunder
#

I opened the script, copy and pasted, and pasted here

keen cedar
crisp thunder
#

oh ok

#
local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")
    
    
    
    local success, currentExperience = pcall(function()
        return experienceStore:GetAsync(player.UserId.."coins")
        end)
        if success then
        coins.Value = currentExperience
        print(currentExperience)
        end
end)
local function savedata()
    game.Players.PlayerRemoving:Connect(function(player)
        local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
        local success, errorMessage = pcall(function()
            experienceStore:SetAsync(player.UserId.."coins", coins.Value)

        end)

        local success2, errorMessage2 = pcall(function()
            experienceStore:UpdateAsync(player.UserId.."coins", coins.Value)
        end)
        if not success2 then
            error(errorMessage2)
        end
        if not success then
            error(errorMessage)

        end

game:BintToClose(function()
    for _,v in pairs(game.Players:GetChildren()) do
                savedata(v)
            end
        end)
    end)
    end
#

isn't this the same?

keen cedar
#

My God...

#

Hold the fuck up... Imma open Roblox Studio and do it for you...

#
local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")



    local success, currentExperience = pcall(function()
        return experienceStore:GetAsync(player.UserId.."coins")
    end)
    if success then
        coins.Value = currentExperience
        print(currentExperience)
    end
end)
local function savedata()
    game.Players.PlayerRemoving:Connect(function(player)
        local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
        local success, errorMessage = pcall(function()
            experienceStore:SetAsync(player.UserId.."coins", coins.Value)

        end)

        local success2, errorMessage2 = pcall(function()
            experienceStore:UpdateAsync(player.UserId.."coins", coins.Value)
        end)
        if not success2 then
            error(errorMessage2)
        end
        if not success then
            error(errorMessage)

        end

        game:BintToClose(function()
            for _,v in pairs(game.Players:GetChildren()) do
                savedata(v)
            end
        end)
    end)
end

Good, now is perfect

#

Also, this doest make sense....

#

Did you ever think why we created a function in the first place?

crisp thunder
#

To call it?

#

And make the function, well, function?

keen cedar
#
local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")

    local success, currentExperience = pcall(function()
        return experienceStore:GetAsync(player.UserId.."coins")
    end)
    if success then
        coins.Value = currentExperience
        print(currentExperience)
    end
end)
function savedata(player)
    local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
    local success, errorMessage = pcall(function()
        experienceStore:SetAsync(player.UserId.."coins", coins.Value)

    end)

    local success2, errorMessage2 = pcall(function()
        experienceStore:UpdateAsync(player.UserId.."coins", coins.Value)
    end)
    if not success2 then
        error(errorMessage2)
    end
    if not success then
        error(errorMessage)
    end
end

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

game:BintToClose(function()
    for _,v in pairs(game.Players:GetChildren()) do
        savedata(v)
    end
end)

This should work

keen cedar
crisp thunder
#

oh wait

#

UNDER

#

not with the player removing

#

I copied and pasted WITH the playerRemoving function

#

while I was supposed to leave it between savedata and bindToClose

keen cedar
crisp thunder
#

I mean, I didn't even thought about checking it, cause u said it will work. I just didn't read it correctly, and copied and pasted the player removing func. I mean I would check if there was game:Destroy() or while true do loop, so it won't crash my game, but in cases like this, I first listen to what others say

#

And, where's the learning when I just copy and paste what others do, well if I am curious of how somthing works, or I don't understand it, I ask people

keen cedar
#

True, but I explained as much and understandable I could and you did that ... thing

crisp thunder
#

And btw, tysm for helping me

#

Oh, and I think that every error is bad (almost), but is this bad? Cause when reducing/adding coins, it saves

#

Error message is the highlighted one

keen cedar
#

Why are you using UpdateAsync? I don't think is needed there

#

Because you already use SetAsync when player leaves/game close

crisp thunder
#

Cause I had problems with setAsync, and I saw that setAsync sets the value for the first time, and update well, updates it

#

can setAsync also update the value? without problems

keen cedar
#

I guess... I'm using only GetAsync to get data and SetAsync to save it

#

So you should just use SetAsync

crisp thunder
#

Oh ok, so I Can just delete the success2 and everything below it, not counting if not success then?

keen cedar
#

Delete everyone that has to do with UpdateAsync

crisp thunder
#

Ok, done

#

Now it again works, but with no errors

#

tysm once again

keen cedar
#

Np

crisp thunder
#

So, imma be closing this post, cya