-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsHandler.luau
More file actions
334 lines (251 loc) · 9.66 KB
/
StatsHandler.luau
File metadata and controls
334 lines (251 loc) · 9.66 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
local TweenService = game:GetService("TweenService")
local propertyTween = nil
local expTween = nil
local player = game.Players.LocalPlayer
local Remotes = game.ReplicatedStorage.Remotes
local Modules = game.ReplicatedStorage.Modules
local StateHandler = require(Modules.StateHandler)
local UIS = game:GetService("UserInputService")
local StatsGui = script.Parent
local successText = StatsGui.SuccessText
local hunger = StatsGui.Hunger
local hygiene = StatsGui.Hygiene
local money = StatsGui.Money
local happiness = StatsGui.Happiness
local EXP = StatsGui.EXP
local level = StatsGui.Level
local petHealthy = false
local petType = nil
local vetVisitCost = 150
local MAX_STAT = 20
local universalProperties = require(Modules.UniversalProperties)
local propertiesModule = nil
Remotes.Eat.OnClientEvent:Connect(function(foodName, petName, food)
if StateHandler.GetState() == "Idle" then
StateHandler.SetState("Eating", 5.5)
food:Destroy()
if propertiesModule == nil then
propertiesModule = require(Modules[petName .. "Properties"])
end
Remotes.Eat:FireServer(propertiesModule.Food[foodName].Happy)
addMoney(propertiesModule.Food[foodName].Money)
if propertiesModule.Food[foodName].Money >= 0 then
successText.Text = "Fed pet " .. foodName .. " (+ " .. propertiesModule.Food[foodName].EXP .. " EXP & +$" .. propertiesModule.Food[foodName].Money .. ")"
else
successText.Text = "Fed pet " .. foodName .. " (+ " .. propertiesModule.Food[foodName].EXP .. " EXP & -$" .. string.sub(propertiesModule.Food[foodName].Money, string.find(propertiesModule.Food[foodName].Money, "-") + 1) .. ")"
end
successText.Visible = true
addHunger(propertiesModule.Food[foodName].Hunger)
addHappiness(propertiesModule.Food[foodName].Happy)
updateEXP(propertiesModule.Food[foodName].EXP)
task.wait(5)
successText.Visible = false
end
end)
Remotes.Clean.OnClientEvent:Connect(function(objectName, petName, object)
if StateHandler.GetState() == "Idle" then
StateHandler.SetState("Cleaning", 5.5)
object:Destroy()
Remotes.Clean:FireServer(universalProperties.Hygiene[objectName].Happy)
successText.Text = "Cleaned pet with " .. objectName .. " (+ " .. universalProperties.Hygiene[objectName].EXP .. " EXP & +$" .. universalProperties.Hygiene[objectName].Money .. ")"
successText.Visible = true
addHygiene(universalProperties.Hygiene[objectName].Hygiene)
addHappiness(universalProperties.Hygiene[objectName].Happy)
updateEXP(universalProperties.Hygiene[objectName].EXP)
task.wait(5)
successText.Visible = false
end
end)
happiness.Changed:Connect(function()
updatePropertyGUI("Happiness")
end)
hygiene.Changed:Connect(function()
updatePropertyGUI("Hygiene")
end)
hunger.Changed:Connect(function()
updatePropertyGUI("Hunger")
end)
money.Changed:Connect(function()
StatsGui.MoneyContainer.MoneyText.Text = "$" .. money.Value
end)
function updatePropertyGUI(property)
propertyTween = TweenService:Create(
StatsGui["StatsContainer"][property.. "Container"][property.."Bar"],
TweenInfo.new(0.7, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),
{Size = UDim2.fromScale((StatsGui[property].Value / MAX_STAT) * 1.001, 1)}):Play()
end
function updateEXP(exp)
EXP:SetAttribute("CurrentEXP", EXP:GetAttribute("CurrentEXP") + exp)
updateEXPGUI(0.6)
task.wait(0.7)
if EXP:GetAttribute("CurrentEXP") >= EXP.Value then
local currEXP = EXP:GetAttribute("CurrentEXP")
EXP:SetAttribute("CurrentEXP", EXP.Value)
updateEXPGUI(0.4)
task.wait(0.4)
EXP:SetAttribute("CurrentEXP", 0)
updateEXPGUI(0)
task.wait(0.4)
level.Value += 1
EXP:SetAttribute("CurrentEXP", currEXP - EXP.Value)
EXP.Value = (5 * level.Value^2) + 20
end
updateEXPGUI(0.6)
end
function updateEXPGUI(tweenTime)
StatsGui.PetName.Text = string.sub(StatsGui.PetName.Text, 1, string.find(StatsGui.PetName.Text, "Lv.", 1, false) - 2) .. "(Lv. " .. level.Value .. ")"
expTween = TweenService:Create(
StatsGui.EXPContainer.EXPBar,
TweenInfo.new(tweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),
{Size = UDim2.fromScale(math.clamp((EXP:GetAttribute("CurrentEXP") / EXP.Value) * 1.001, 0, 1.001), 1)}
):Play()
-- StatsGui.EXPContainer.EXPBar.Size = UDim2.fromScale((EXP:GetAttribute("CurrentEXP") / EXP.Value) * 1.001, 1)
end
--FUNCTIONS
function addHygiene(hygieneAdd)
hygiene.Value += hygieneAdd
if hygiene.Value > MAX_STAT then
hygiene.Value = MAX_STAT
elseif hygiene.Value < 0 then
hygiene.Value = 0
end
end
function addHunger(hungerAdd)
hunger.Value += hungerAdd
if hunger.Value > MAX_STAT then
hunger.Value = MAX_STAT
elseif hunger.Value < 0 then
hunger.Value = 0
end
end
function addHappiness(happy)
happiness.Value += happy
if happiness.Value > MAX_STAT then
happiness.Value = MAX_STAT
elseif happiness.Value < 0 then
happiness.Value = 0
end
end
function addMoney(moneyAdded)
money.Value += moneyAdded
StatsGui.MoneyContainer.MoneyText.Text = "$" .. money.Value
end
function initializeStats()
money.Value = 170
hunger.Value = 10
hygiene.Value = 10
happiness.Value = 10
game.ReplicatedStorage.Remotes.GetFood:FireServer("destroy")
game.ReplicatedStorage.Remotes.GetHygiene:FireServer("destroy")
updatePropertyGUI("Happiness")
updatePropertyGUI("Hunger")
updatePropertyGUI("Hygiene")
addMoney(0)
EXP.Value = (5 * level.Value^2) + 20
updateEXPGUI(0.6)
petHealthy = true
end
StatsGui.EmergencyContainer.Pay.MouseButton1Up:Connect(function()
if StatsGui.EmergencyContainer.Pay.Text == "Pay" then
task.wait(0.1)
if money.Value >= vetVisitCost then
StatsGui.EmergencyContainer.Visible = false
StatsGui.StatsContainer.Visible = true
StatsGui.MoneyContainer.Visible = true
StatsGui.EXPContainer.Visible = true
game.ReplicatedStorage.Remotes.LostEvent:Fire("paid")
money.Value -= vetVisitCost
hunger.Value = 10
hygiene.Value = 10
happiness.Value = 10
updatePropertyGUI("Happiness")
updatePropertyGUI("Hunger")
updatePropertyGUI("Hygiene")
addMoney(0)
EXP.Value = (5 * level.Value^2) + 20
updateEXPGUI(0.6)
petHealthy = true
drainStats()
else
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like you are in debt. Game over!"
StatsGui.EmergencyContainer.Pay.Text = "Return"
game.ReplicatedStorage.Remotes.InitializePet:FireServer("destroy")
end
else
task.wait(0.1)
game.ReplicatedStorage.Remotes.LostEvent:Fire("restart")
StatsGui.EmergencyContainer.Visible = false
player.PlayerGui.StarterGui.Enabled = true
StatsGui.EmergencyContainer.Pay.Text = "Pay"
end
end)
function drainStats()
task.spawn(function()
while petHealthy do
if StateHandler.GetState() == "Idle" then
addHunger(-1)
addHygiene(-1)
addHappiness(-1)
if StatsGui.Hygiene.Value <= 0 or StatsGui.Happiness.Value <= 0 or StatsGui.Hunger.Value <= 0 then
StatsGui.StatsContainer.Visible = false
StatsGui.MoneyContainer.Visible = false
StatsGui.EXPContainer.Visible = false
StatsGui.EmergencyContainer.Balance.Text = "Current Balance: $" .. money.Value
petHealthy = false
game.ReplicatedStorage.Remotes.LostEvent:Fire("lost")
StatsGui.EmergencyContainer.Visible = true
if StatsGui.Hygiene.Value <= 0 then
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like your pet has gotten an infection. You will need to pay the medical costs to get that fixed!"
elseif StatsGui.Happiness.Value <= 0 then
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like your pet is depressed. You will need to pay the medical costs to get that fixed!"
else
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like your pet is ill. You will need to pay the medical costs to get that fixed!"
end
end
end
wait(30)
end
end)
end
game.ReplicatedStorage.Remotes.InitializePet.OnClientEvent:Connect(function(pet)
propertiesModule = nil
petType = pet
StatsGui.StatsContainer.Visible = true
StatsGui.MoneyContainer.Visible = true
StatsGui.EXPContainer.Visible = true
initializeStats()
drainStats()
end)
local killKeys = {
Enum.KeyCode.LeftBracket,
Enum.KeyCode.RightBracket,
Enum.KeyCode.BackSlash
}
UIS.InputEnded:Connect(function(input, gpe)
if table.find(killKeys, input.KeyCode) then
if input.KeyCode == Enum.KeyCode.LeftBracket then
addHunger(-30)
elseif input.KeyCode == Enum.KeyCode.RightBracket then
addHygiene(-30)
elseif input.KeyCode == Enum.KeyCode.BackSlash then
addHappiness(-30)
end
task.wait(0.5)
if StatsGui.Hygiene.Value <= 0 or StatsGui.Happiness.Value <= 0 or StatsGui.Hunger.Value <= 0 then
StatsGui.StatsContainer.Visible = false
StatsGui.MoneyContainer.Visible = false
StatsGui.EXPContainer.Visible = false
StatsGui.EmergencyContainer.Balance.Text = "Current Balance: $" .. money.Value
petHealthy = false
game.ReplicatedStorage.Remotes.LostEvent:Fire("lost")
StatsGui.EmergencyContainer.Visible = true
if StatsGui.Hygiene.Value <= 0 then
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like your pet has gotten an infection. You will need to pay the medical costs to get that fixed!"
elseif StatsGui.Happiness.Value <= 0 then
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like your pet is depressed. You will need to pay the medical costs to get that fixed!"
else
StatsGui.EmergencyContainer.ProblemText.Text = "Looks like your pet is ill. You will need to pay the medical costs to get that fixed!"
end
end
end
end)