the wins gets set to the wins of the player with the least wins.
my code:
local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local RunService = game:GetService("RunService")
local database = DatastoreService:GetDataStore(script.CurrentDataStore.Value)
local sessionData = {}
local defaultData = {
--leaderstats
["Wins"] = 0,
--items
["Cheese"] = false,
--towers
["Anime"] = true,
["Superhero"] = false,
["Artist"] = false,
["Cartoon"] = false,
}
function PlayerAdded(player)
local character = player.Character or player.CharacterAdded:Wait()
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
if not playerData then
playerData = defaultData
end
sessionData[player.UserId] = playerData
else
warn("Unable to get Data for", player.UserId)
player:Kick("Unable to load your data. Try again later")
end
createdata(player)
end
Players.PlayerAdded:Connect(PlayerAdded)
function PlayerLeaving(player)
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for", player.Name)
else
warn("Unable to save for", player.Name)
end
end
end
Players.PlayerRemoving:Connect(PlayerLeaving)
function ServerShutdown()
if RunService:IsStudio() then
return
end
print("Server Shutting Down!")
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
PlayerLeaving(player)
end)
end
end
game:BindToClose(ServerShutdown)
function createdata(player)
--create values
local codes = Instance.new("Folder")
codes.Name = "RedeemedCodes"
codes.Parent = player
-- leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
local playerdata = Instance.new("Folder")
playerdata.Name = "PlayerData"
playerdata.Parent = player
-- items
local itemfolder = Instance.new("Folder")
itemfolder.Name = "Item"
itemfolder.Parent = playerdata
local cheesevalue = Instance.new("BoolValue")
cheesevalue.Name = "cheese"
cheesevalue.Parent = itemfolder
-- tower
local towerfolder = Instance.new("Folder")
towerfolder.Name = "Tower"
towerfolder.Parent = playerdata
local animevalue = Instance.new("BoolValue")
animevalue.Name = "anime"
animevalue.Parent = towerfolder
local superherovalue = Instance.new("BoolValue")
superherovalue.Name = "superhero"
superherovalue.Parent = towerfolder
local artistvalue = Instance.new("BoolValue")
artistvalue.Name = "artist"
artistvalue.Parent = towerfolder
local cartoonvalue = Instance.new("BoolValue")
cartoonvalue.Name = "cartoon"
cartoonvalue.Parent = towerfolder
--set values
wins.Value = sessionData[player.UserId].Wins
wins.Changed:Connect(function()
sessionData[player.UserId].Wins = wins.Value
end)
cheesevalue.Value = sessionData[player.UserId].Cheese
cheesevalue.Changed:Connect(function()
sessionData[player.UserId].Cheese = cheesevalue.Value
end)
animevalue.Value = sessionData[player.UserId].Anime
animevalue.Changed:Connect(function()
sessionData[player.UserId].Anime = animevalue.Value
end)
superherovalue.Value = sessionData[player.UserId].Superhero
superherovalue.Changed:Connect(function()
sessionData[player.UserId].Superhero = superherovalue.Value
end)
artistvalue.Value = sessionData[player.UserId].Artist
artistvalue.Changed:Connect(function()
sessionData[player.UserId].Artist = artistvalue.Value
end)
cartoonvalue.Value = sessionData[player.UserId].Cartoon
cartoonvalue.Changed:Connect(function()
sessionData[player.UserId].Cartoon = cartoonvalue.Value
end)
-- codes
if sessionData[player.UserId].Codes then
for _, redeemedCode in sessionData[player.UserId].Codes do
local newCode = Instance.new("StringValue")
newCode.Name = redeemedCode
newCode.Parent = player:WaitForChild("RedeemedCodes")
end
end
codes.ChildAdded:Connect(function()
local inventory = {}
for _, code in player.RedeemedCodes:GetChildren() do
table.insert(inventory, code.Name)
end
sessionData[player.UserId].Codes = inventory
end)
end
-- requests
function GetObject(fullName)
local segments = fullName:split(".")
local current = game
for _,location in pairs(segments) do
current = current[location]
end
return current
end
game:GetService("ReplicatedStorage").RequestValue.OnServerEvent:Connect(function(player, bool, button)
local realbutton = GetObject(button)
local price = realbutton:GetAttribute("Price")
if player.leaderstats.Wins.Value >= price then
player.leaderstats.Wins.Value -= price
bool.Value = true
end
end)
game:GetService("ReplicatedStorage").ModifyItem.OnServerEvent:Connect(function(player, item, equipping, id)
local bool = player.PlayerData.Item:FindFirstChild(string.lower(item))
if bool and bool.Value == true or MarketplaceService:UserOwnsGamePassAsync(player.UserId, id) then
if equipping then
local newitem = game:GetService("ServerStorage").Tools:WaitForChild(item):Clone()
newitem.Parent = player.Backpack
require(game:GetService("ReplicatedStorage").Notification).Notify(player, "Successfully Equipped!", 3, Color3.new(0,1,0))
else
local baditem = player.Backpack:FindFirstChild(item) or player.Character:FindFirstChild(item)
if baditem then
baditem:Destroy()
require(game:GetService("ReplicatedStorage").Notification).Notify(player, "Successfully Unequipped!", 3, Color3.new(0,1,0))
end
end
end
end)