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

Data store service script doesn't work?

Asked by 4 years ago

Hello!

I've been working on a data store script, but when I changed my cash value and test played again, it didn't work. I've checked the script for errors, and there are none, it also didnt print anything in the output, I have API Services on, and still dosent work, even published it.

Here's script:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

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

    local cash = Instance.new("IntValue", leaderstats)
    cash.Name = "Points"

    local playerUserId  = "Player_"..plr.UserId
    print(playerUserId)

    --Loads data

    local data
    local success, errormessage = pcall(function()
     data = myDataStore:GetAsync(playerUserId)
    end)

    if success then
        --Set data equal to current cash
        cash.Value = data
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId
    print(playerUserId)

    local data = plr.leaderstats.Points.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

    if success then
        print("Data saved")
    else
        print("Save error")
        warn(errormessage)
    end
end)

2 answers

Log in to vote
0
Answered by
uhSaxlra 181
4 years ago

Even if it does load the data succesfully, it doesn't mean the data is a integer. It could still be nil. Try checking if it's not nil first.

if success and data ~= nil then
        --Set data equal to current cash
        cash.Value = data
end

Before you test this, make sure to start with a new datastore to prevent loading in previous data.

local myDataStore = DataStoreService:GetDataStore("myDataStore2")

Hope this helps :D

0
Lemme try that really quick. epic_4gaming 25 — 4y
0
Erased the old Mydatastore and replaced it, and replaced the if statement too epic_4gaming 25 — 4y
0
Did it work? uhSaxlra 181 — 4y
0
Tried it. Didn't work, I changed the points value too epic_4gaming 25 — 4y
View all comments (23 more)
0
Are you trying this in studio? uhSaxlra 181 — 4y
0
Yes. epic_4gaming 25 — 4y
0
Try doing a live test, sometimes studio is buggy uhSaxlra 181 — 4y
0
Ya mean join in client, not studio? epic_4gaming 25 — 4y
0
Are you changing the points value from the client side or server side. Client side won't save. uhSaxlra 181 — 4y
0
I changed the value from the server side in studio epic_4gaming 25 — 4y
0
But did you meant try it in roblox client or studio? epic_4gaming 25 — 4y
0
Okay, try and do a live test uhSaxlra 181 — 4y
0
Roblox client uhSaxlra 181 — 4y
0
Ohh, I understand you now. epic_4gaming 25 — 4y
0
kkkkkk uhSaxlra 181 — 4y
0
Still didn't work in roblox client, didnt try it in studio. Maybe DataStoreService is down? epic_4gaming 25 — 4y
0
That'd be extremely bad for every roblox game, so I'm doubting that uhSaxlra 181 — 4y
0
I know, but then how does the script dosen't work? I checked for errors, but 1 red flag is nothing printed in output epic_4gaming 25 — 4y
0
Replace line 35 with this (Idk my gut is telling me something): myDataStore:SetAsync(playerUserId, plr.leaderstats.Points.Value) uhSaxlra 181 — 4y
0
Nothing. epic_4gaming 25 — 4y
0
Well ,if someone wants to give up right now, we can probably agree on a point that DataStoreService is down. epic_4gaming 25 — 4y
0
rip uhSaxlra 181 — 4y
0
Well, if you want to give up, well, thank you for trying. epic_4gaming 25 — 4y
0
Do you mind putting the full script and your exact method of trying it? uhSaxlra 181 — 4y
0
Alright, I first tested it with studio client, then tested it on roblox client, then nothing happened, I changed the points value by explorer in studio, then developer console in roblox client. Full script is below epic_4gaming 25 — 4y
0
How are you changing your cash value exactly? royaltoe 5144 — 4y
0
Conclusion: Data stores don't work in studio, even if api services is on.... uhSaxlra 181 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore2")

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

    local cash = Instance.new("IntValue", leaderstats)
    cash.Name = "Points"

    local playerUserId  = "Player_"..plr.UserId
    print(playerUserId)

    --Loads data

    local data
    local success, errormessage = pcall(function()
     data = myDataStore:GetAsync(playerUserId)
    end)

    if success and data ~= nil then
        --Set data equal to current cash
        cash.Value = data
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId
    print(playerUserId)

    local data = plr.leaderstats.Points.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, plr.leaderstats.Points.Value)
    end)

    if success then
        print("Data saved")
    else
        print("Save error")
        warn(errormessage)
    end
end)

0
Get this: I copied the exact script, put it in my game, ran a live test and it works fine. Here it is: https://www.roblox.com/games/4589359421/uhSaxlras-Place-Number-160 uhSaxlra 181 — 4y
0
games not public, but i am glad you got it working royaltoe 5144 — 4y
0
WHAT HOW THAT IS IMPOSSIBLE epic_4gaming 25 — 4y
0
Hold up uhSaxlra 181 — 4y
0
Public uhSaxlra 181 — 4y

Answer this question