-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex
More file actions
92 lines (78 loc) · 2.14 KB
/
Copy pathComplex
File metadata and controls
92 lines (78 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
local CoinSystem = {}
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local CoinStore = DataStoreService:GetDataStore("PlayerCoins")
local PlayerData = {}
local function DefaultData()
return {
Coins = 0,
Multipliers = {
VIP = 1,
Combo = 1,
Event = 1
},
Streak = 0
}
end
local function LoadPlayer(player)
local data
local success, err = pcall(function()
data = CoinStore:GetAsync(player.UserId)
end)
if success and data then
PlayerData[player.UserId] = data
else
PlayerData[player.UserId] = DefaultData()
end
end
local function SavePlayer(player)
local data = PlayerData[player.UserId]
if data then
local success, err = pcall(function()
CoinStore:SetAsync(player.UserId, data)
end)
if not success then warn("Failed saving coins for "..player.Name..": "..err) end
end
end
function CoinSystem.AddCoins(player, amount)
local data = PlayerData[player.UserId]
if not data then return end
local totalMultiplier = data.Multipliers.VIP * data.Multipliers.Combo * data.Multipliers.Event
local finalAmount = math.floor(amount * totalMultiplier)
data.Coins = data.Coins + finalAmount
data.Streak = data.Streak + 1
end
function CoinSystem.SetMultiplier(player, type, value, duration)
local data = PlayerData[player.UserId]
if not data then return end
data.Multipliers[type] = value
if duration then
delay(duration, function()
data.Multipliers[type] = 1
end)
end
end
function CoinSystem.GetCoins(player)
local data = PlayerData[player.UserId]
if data then
return data.Coins
else
return 0
end
end
Players.PlayerAdded:Connect(function(player)
LoadPlayer(player)
end)
Players.PlayerRemoving:Connect(function(player)
SavePlayer(player)
PlayerData[player.UserId] = nil
end)
spawn(function()
while true do
wait(300)
for _, player in pairs(Players:GetPlayers()) do
SavePlayer(player)
end
end
end)
return CoinSystem