简体   繁体   中英

Why LeaderStats folder doesn`t creates| Roblox Studio, Lua

My leaderstats script doesnt work for some reason. I dont know why. When i testing game in roblox studio folder just doesnt creating. Because of the bug some information doesn`t loads.

There is the code (Script located in ServerScriptStorage):

wait(1)
print("Script LeaderStats started!")

local dataStoreService = game:GetService("DataStoreService")
local clicksDataStore = dataStoreService:GetDataStore("Clicks")


game.Players.PlayerAdded:Connect(function(Player)
    
    local PStats = Instance.new("Folder", Player)
    PStats.Name = "PStats"
    
    
    
    local clicks = Instance.new("IntValue", PStats)
    clicks.Name = "Clicks"
    clicks.Value = 0
    
    
    local playerUserId = "player_"..Player.UserId
    
    -- loading data
    local clicksData
    
    
    local success, errormessage = pcall(function()
        clicksData = clicksDataStore:GetAsync(playerUserId, clicksValue)

    end)

    if success then
        clicks.Value = clicksData
    end
    



end)

-- saving data
game.Players.PlayerRemoving:Connect(function(Player)
    local playerUserId = "player_"..Player.UserId
    local clicksValue = Player.PStats.Clicks.Value
    local success, errormessage = pcall(function()
        clicksDataStore:SetAsync(playerUserId, clicksValue)
    end)
end)



game:BindToClose(function(Player)
    for _, Player in pairs(game.Players:GetPlayers()) do
        local playerUserId = "player_"..Player.UserId
        local clicksValue = Player.PStats.Clicks.Value
        local success, errormessage = pcall(function(Player)
            clicksDataStore:SetAsync(playerUserId, clicksValue)
        end)
    end

end)

I tried putting it in workspace, but folder didn`t create.

Make sure it's a Server Script inside of ServerScriptService.

replace line 31 with

clicksData = clicksDataStore:GetAsync(playerUserId)

If you want it to show as a leaderboard, replace line 15 with

PStats.Name = "leaderstats"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM