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

How to save data when the player leaves?

Asked by 7 years ago

Heres the script that i have but when i print the datastore i get nil:

Script:

Data = game:GetService("DataStoreService"):GetDataStore("BlastOffMoons")
JSON = game:GetService("HttpService")
Tycoon = ''
local base
local corner
local placedItems = {}
game.Players.PlayerAdded:connect(function(Player)
    repeat wait(1) until Player.Character
    repeat wait() until workspace.TestTycoon.Owner.Value ~= ''
    if workspace.TestTycoon.Owner.Value == Player.Name then
        Tycoon = workspace.TestTycoon
    end
    local plrData = Data:GetAsync(Player.UserId)
    print(plrData)
    if plrData then
    for i, Object in pairs(game:GetService("ReplicatedStorage").GameItems:GetChildren()) do
        for e, Data in pairs(plrData:GetChildren()) do
            if Object.Name == Data[1] then
                local newObject = Object:Clone()
                newObject.Parent =  Tycoon.TestItems
                local location = tonumber(Data[2],Data[3],Data[4])
                newObject:SetPrimaryPartCFrame(location)
            end
        end
    end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    if workspace.TestTycoon.Owner.Value == plr.Name then
        Tycoon = workspace.TestTycoon
        base = Tycoon.Base
        corner = Tycoon.Base.Position - (Tycoon.Base.Size/3)
        print(corner)
    end
    local playerData = {
    Money = plr.Money.Value,
    Placed = {['itemPositions:'] = {}}
    }
    Tycoon = workspace.TestTycoon
    for i, v in pairs(Tycoon.TestItems:GetChildren()) do
        print(v.ClassName)
        if v:IsA('Model') then
            table.insert(playerData.Placed["itemPositions:"], {v.Name, v.PrimaryPart.Position.x,v.PrimaryPart.Position.y, v.PrimaryPart.Position.z})
        print(JSON:JSONEncode(playerData))
        Data:SetAsync(plr.UserId, playerData)
        end
    end
end)

0
You could always store data in a folder in ServerStorage and then, when they leave, even if they take the player character with them it leaves the data. You can then save the values in the folder under their name and done :). It's what I usually do. Note: Delete it after as it otherwise builds up data usage. CodeOnRBLX 1 — 7y
0
I really reccommend you to convert your script to FE, there are many vunrabilities. And there are a few ways you can do this, you could have an autosave feature. StoleYourClothes 105 — 7y
0
Thats why it isnt working i have FE enabled so do i need remote events or something? therealae 7 — 7y

Answer this question