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

Why is my data store not recognising the Value of my IntValue?

Asked by 6 years ago
Edited 6 years ago

Ok, so my first issue is the Data Store not updating when a Variable changes. The current code I have for this is

    var1.Changed:Connect(function()
        print("Saving. . .")
        data1:SetAsync(player.UserId, var1.Value)
    print(player.Name.. "'s data of "..var1.Value.." "..var1.Name.." has been saved!")
    end)

To me, it seemed like it wasn't recognising that the Value had changed because it never printed 'Saving...' but I can't understand why...

Second of all the data never saves when the player leaves the game.

game.Players.PlayerRemoving:Connect(function(player)
    print("Saving. . .")
    data1:SetAsync(player.UserId, player.leaderstats.Credits.Value)
    print(player.Name.. "'s data of "..var1.Value.." "..var1.Name.." has been saved!")
end)

This prints out PureGamerGames's data of 0 Credits has been saved! however, I never set the Value to 0.

The only possible solution I thought maybe the issue was the game not recognising var1, however, I have it declared here.

    local var1 = Instance.new("IntValue", stats)
    var1.Name = "Credits"
    var1.Value = data1:GetAsync(player.UserId, var1.Value) or 0
    data1:SetAsync(player.UserId, var1.Value)

One thing to note is that I have API access enabled and I was testing this out in play test..

1 answer

Log in to vote
0
Answered by
HaveASip 494 Moderation Voter
6 years ago
Edited 6 years ago

Hey there! Your script is difficult to read and everything is scattered. I hope you will not mind if I write a new script and explain. :D

For first insert a script into ServerScriptService and type in:

local DataStore = game:GetService("DataStoreService")
local CreditsData = DataStore:GetDataStore("Credits") -- gets data store for credits

game.Players.PlayerAdded:Connect(function(player) -- starts when player joins
    local leaderstats = Instance.new("Folder") -- creating folder inside of player
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"
    local Credits =     Instance.new("IntValue") -- creating credits inside of leaderstats
    Credits.Paraent = leaderstats
    Credits.Name = "Credits"
    Credits.Value = 0   

    local PlrData = CreditsData:GetAsync(player.UserId) -- getting data from datastore

    if PlrData ~= nil then -- checking if player already have data or not
        Credits.Value = PlrData -- set credits value the value from datastore
        print("Successfuly loaded " .. player.Name .. " 's data. Got " .. PlrData .. " credits from datastore.")
    else -- if player has not data in datastore then:
        CreditsData:SetAsync(player.UserId, Credits.Value) -- saves information into datastore
    end
end)

game.Players.PlayerRemoving:Connect(function(player) -- starts when player leave
    CreditsData:SetAsync(player.UserId, player.leaderstats.Credits.Value) -- saves data
end)

I recommend to use ":UpdateAsync()" when you want to update your data store information, but player still doesnt leave

Here is example:

local DataStore = game:GetService("DataStoreService")
local CreditsData = DataStore:GetDataStore("Credits") -- gets data store for credits

game.Players.PlayerAdded:Connect(function(player) -- starts when player joins
    local leaderstats = Instance.new("Folder") -- creating folder inside of player
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"
    local Credits =     Instance.new("IntValue") -- creating credits inside of leaderstats
    Credits.Paraent = leaderstats
    Credits.Name = "Credits"

    Credits.Value.Changed:Connect(function() --starts when credits value changed
        CreditData:UpdateAsync(player.UserId) -- updating value
    end)
end)
0
Thank you, just modified it a little bit. PureGamerGames 16 — 6y
Ad

Answer this question