File size: 4,108 Bytes
0712d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- src/ServerScriptService/PlayerSetupManager.server.lua

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local GameConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("GameConfig"))
local EconomyConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("EconomyConfig"))
local ShopConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("ShopConfig"))
local NotificationEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("NotificationEvent")

-- Player data cache for this session
local playerData = {}

local function createLeaderstats(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = GameConfig.Stats.Cash
	cash.Value = EconomyConfig.StarterCash
	cash.Parent = leaderstats

	local woodChopped = Instance.new("IntValue")
	woodChopped.Name = GameConfig.Stats.WoodChopped
	woodChopped.Value = 0
	woodChopped.Parent = leaderstats
end

local function initializeInventory(player)
	-- Start with empty inventory -- players must craft their tools
	local defaultInventory = {
		Tools = {},
		Machines = {},
		Vehicles = {},
		Automation = {},
	}

	-- Store in player data cache
	playerData[player.UserId] = {
		Inventory = defaultInventory,
		EquippedAxe = "", -- empty = fists
		QuestProgress = {},
		Achievements = {},
		BiomesVisited = {},
		Resources = { Wood = 0, Stone = 0, GoldOre = 0, Diamond = 0 },
		TotalEarned = 0,
		TotalWoodSold = 0,
		TotalBuilt = 0,
	}

	-- Empty string means fists -- client will default to fist stats
	player:SetAttribute("EquippedAxe", "")
end

local function onPlayerAdded(player)
	createLeaderstats(player)
	initializeInventory(player)

	-- Wait for character to load then set attributes
	player.CharacterAdded:Connect(function(character)
		character:SetAttribute("EquippedAxe", playerData[player.UserId].EquippedAxe)
	end)

	-- Welcome notification
	task.delay(3, function()
		if player.Parent then
			NotificationEvent:FireClient(player, "Welcome", "Welcome to Timberbound Expeditions! Punch trees to gather wood, then press C to craft your first axe!")
		end
	end)
end

local function onPlayerRemoving(player)
	playerData[player.UserId] = nil
end

-- Expose playerData globally for other server scripts
_G.GetPlayerData = function(player)
	return playerData[player.UserId]
end

_G.SetPlayerData = function(player, key, value)
	if playerData[player.UserId] then
		playerData[player.UserId][key] = value
	end
end

_G.AddToInventory = function(player, category, itemId)
	local data = playerData[player.UserId]
	if data and data.Inventory[category] then
		table.insert(data.Inventory[category], itemId)
		return true
	end
	return false
end

_G.HasInInventory = function(player, category, itemId)
	local data = playerData[player.UserId]
	if data and data.Inventory[category] then
		for _, id in ipairs(data.Inventory[category]) do
			if id == itemId then return true end
		end
	end
	return false
end

_G.IncrementStat = function(player, statName, amount)
	local data = playerData[player.UserId]
	if not data then return end

	if statName == "WoodChopped" then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local stat = leaderstats:FindFirstChild(statName)
			if stat then
				stat.Value = stat.Value + amount
			end
		end
	elseif statName == "TotalEarned" then
		data.TotalEarned = (data.TotalEarned or 0) + amount
	elseif statName == "TotalWoodSold" then
		data.TotalWoodSold = (data.TotalWoodSold or 0) + amount
	elseif statName == "TotalBuilt" then
		data.TotalBuilt = (data.TotalBuilt or 0) + amount
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

-- Handle players who joined before this script loaded
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(function()
		onPlayerAdded(player)
	end)
end