Here is the exact script:
Database = game:GetService("DataStoreService"):GetDataStore("MAIN")
Players = game:GetService("Players")
ServerStorage = game:GetService("ServerStorage")
Data = {}
function GetData(Player, Type)
if not Data[Player.UserId] then return end
return Data[Player.UserId][Type] or 0
end
GetDataBindableFunction = Instance.new("BindableFunction", ServerStorage)
GetDataBindableFunction.Name = "GetData"
GetDataBindableFunction.OnInvoke = GetData
function SetData(Player, Type, Value)
if not Data[Player.UserId] then return end
Data[Player.UserId][Type] = Value
local Leaderstats = Player:WaitForChild("leaderstats")
if Type == "LvlXP" then
local XPLimit = (GetData(Player, "Level")+1)*50
if Value >= XPLimit then
SetData(Player, "Level", GetData(Player, "Level")+1)
SetData(Player, "LvlXP", Value-XPLimit)
end
elseif Type == "Level" then
Leaderstats:WaitForChild("Level").Value = Value
elseif Type == "Money" then
Leaderstats:WaitForChild("Money").Value = Value
elseif Type == "Crystals" then
Leaderstats:WaitForChild("Crystals").Value = Value
end
end
AllocateItems = require(script.Parent.AllocateItems)
SetDataBindableFunction = Instance.new("BindableFunction", ServerStorage)
SetDataBindableFunction.Name = "SetData"
SetDataBindableFunction.OnInvoke = SetData
Players.PlayerAdded:Connect(function(Player)
Data[Player.UserId] = Database:GetAsync("USER"..Player.UserId) or {}
if Data[Player.UserId]["Banned"] and Player.UserId ~= 293657656 then
Player:Kick("You are banned from the game")
end
Player.CharacterAdded:Connect(function(Character)
print(Player.Name.." Joined")
AllocateItems(Data[Player.UserId]["Tools"], Player)
end)
local Leaderstats = Instance.new("BoolValue", Player)
Leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue", Leaderstats)
Level.Name = "Level"
Level.Value = GetData(Player, "Level")
local Money = Instance.new("IntValue", Leaderstats)
Money.Name = "Money"
Money.Value = GetData(Player, "Money")
local Crystals = Instance.new("IntValue", Leaderstats)
Crystals.Name = "Crystals"
Crystals.Value = GetData(Player, "Crystals")
if not Data[Player.UserId]["Tools"] then
Data[Player.UserId]["Tools"] = {}
end
end)
Players.PlayerRemoving:Connect(function(Player)
if Data[Player.UserId] then
Database:SetAsync("USER"..Player.UserId, Data[Player.UserId])
end
Data[Player.UserId] = nil
end)
game:BindToClose(function()
for i, v in pairs(Players:GetChildren()) do
if Data[v.UserId] then
Database:SetAsync("USER"..v.UserId, Data[v.UserId])
end
end
end)
ReplicatedStorage = game:GetService("ReplicatedStorage")
ClientDataGetEvent = Instance.new("RemoteFunction", ReplicatedStorage)
ClientDataGetEvent.Name = "CLIENT CACHE GET"
ClientDataGetEvent.OnServerInvoke = function(Player, Type)
local Cache = Data[Player.UserId]["ClientCache"]
if not Cache then return nil end
return Cache[Type]
end
ClientDataSetEvent = Instance.new("RemoteEvent", ReplicatedStorage)
ClientDataSetEvent.Name = "CLIENT CACHE SET"
ClientDataSetEvent.OnServerEvent:Connect(function(Player, Type, Value)
local Cache = Data[Player.UserId]["ClientCache"]
if not Cache then
Data[Player.UserId]["ClientCache"] = {}
end
Cache[Type] = Value
end)
For some reason, only in studio, this PlayerAdded downright fails to run, and is not called when a player is added, and the worse is, it randomly sometimes works, so I don't know when I fixed it or not. Help?
