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

why the folder that i wanted to create in the player is not created?

Asked by 3 years ago
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local function playerAdded(player)
    local TycoonSaveFolder = Instance.new ("Folder")
    TycoonSaveFolder.Name = "TycoonSaveFolder"

    local leaderstats = Instance.new ("Folder")
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue")

    money.Name = "Money"
    money.Parent = leaderstats

    local points = Instance.new("IntValue")

    points.Name = "Points"
    points.Parent = leaderstats

    local rebirth = Instance.new("IntValue")
    rebirth.Name = "Rebirths"
    rebirth.Parent = leaderstats
    leaderstats.Parent = player

    local playerUserId = 'Player_'..player.UserId
    local data = playerData:GetAsync(playerUserId)
    if data then
        money.Value = data["Money"]
        points.Value = data["Points"]
        rebirth.Value = data["Rebirths"]
    else
        money.Value = 0
        points.Value = 0
        rebirth.Value = 0
    end
end

local function create_table(player)
    local player_stats = {}
    for i, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function onPlayerExit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserID = 'Player_'..player.UserId
        playerData:SetAsync(playerUserID, player_stats)
    end)

    if not success then
        warn("Could not save data")
    end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(onPlayerExit)

everything is working except for the

    local TycoonSaveFolder = Instance.new ("Folder")
    TycoonSaveFolder.Name = "TycoonSaveFolder"
0
Instance.new() doesn't assign a Parent by itself. You should assign the Instance's Parent after you assign every other wanted value. DeceptiveCaster 3761 — 3y
0
u forgot to assign the parent speedyfox66 237 — 3y
0
but how come the leaderstats folder can be created? Struggage 10 — 3y
0
ohh and how do you assign a player as a parent? Struggage 10 — 3y
View all comments (4 more)
0
Type: TycoonSaveFolder.Parent = player RektwayYTB 123 — 3y
0
thank youu!! :D Struggage 10 — 3y
0
Another way to assign the parent would be: local TycoonSaveFolder = Instance.new ("Folder",(put what you want TycoonSaveFolder's parent to be in here)) COOLGUY16T 37 — 3y
0
thanks :D Struggage 10 — 3y

Answer this question