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

DataReady Not Working?

Asked by 8 years ago

I am an beginner-intermediate scripter, and I am new to the DataReady, until today. I looked on the ROBLOX wiki for a template on what I need to do to make a IntValue inside a folder that keeps track of how many times you have played.

vcKey = "PlayerVisitCount"

game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() then

        local ls = Instance.new("Folder")
        ls.Name = "Visits"
        ls.Parent = player

        local vc = Instance.new("IntValue")
        vc.Name = "VisitsCount"
        vc.Parent = ls
        vc.Value = player:LoadNumber(vcKey) + 1
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    if player:FindFirstChild("Visits") then
        player:SaveNumber(vcKey, player.Visits.VisitsCount.Value)    
    end
end)

When I run it, the folder doesn't even appear inside the player. When I check the output window to see an error, there are no errors either.

2 answers

Log in to vote
0
Answered by
Juddily 33
8 years ago

Be aware that in test mode on studio, the player loads before the scripts. This means that your player is added to the game before your script has a chance to recognize it. I would do something like this:

vcKey = "PlayerVisitCount"

local function AddPlayer(player)
    if player:WaitForDataReady() then
            local ls = Instance.new("Folder")
            ls.Name = "Visits"
            ls.Parent = player

            local vc = Instance.new("IntValue")
            vc.Name = "VisitsCount"
            vc.Parent = ls
            vc.Value = player:LoadNumber(vcKey) + 1
    end
end

for _,v in pairs(game.Players:GetChildren()) do
    AddPlayer(v)
end

game.Players.PlayerAdded:connect(AddPlayer)

game.Players.PlayerRemoving:connect(function(player)
    if player:FindFirstChild("Visits") then
        player:SaveNumber(vcKey, player.Visits.VisitsCount.Value)   
    end
end)

Also, don't quote me on this, but i'm not certain that data stores will work in test mode. If that code still isn't working, try printing to the dev console online.

Ad
Log in to vote
0
Answered by 8 years ago

:WaitForDataReady() is a yield method of player. Get rid of the if statement, and put the :WaitForDataReady() on its own, then it should work.

However, I advise strongly against DataPersistence, you should switch to the latest and more reliable DataStore's.

Answer this question