Hello!
One of my main desires during the production of my TD game is for skins. I have currently been thinking, restarting and clearing my mind each day on how to make it. I have all the skins and my idea for accessing it has sort of been:
I can’t really think of any ways on how to do this. My TD game is in full working condition but its mainly just adding the skins.
Heres one of the scripts that manages the shop…
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Events = ReplicatedStorage:WaitForChild("Events")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Functions = ReplicatedStorage:WaitForChild("Functions")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local database = DataStoreService:GetDataStore("teststore1")
local towers = require(Modules:WaitForChild("TowerShop"))
local MAX_SELECTED_TOWERS = 5
local data = {}
-- Load the players data
local function LoadData(player)
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()
end
until success or attempt == 3
if success then
print("Connection success")
if not playerData then
print("New player, giving default data")
playerData = {
["Bucks"] = 100,
["SelectedTowers"] = {"Rookie"},
["OwnedTowers"] = {"Rookie"},
}
end
data[player.UserId] = playerData
else
warn("Unable to get data for player", player.UserId)
player:Kick("There was a problem getting your data")
end
end
Players.PlayerAdded:Connect(LoadData)
-- Save the players data
local function SaveData(player)
if data[player.UserId] then
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:UpdateAsync(player.UserId, function()
return data[player.UserId]
end)
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 3
if success then
print("Data saved successfully")
else
warn("Unable to save data for", player.UserId)
end
else
warn("No session data for", player.UserId)
end
end
Players.PlayerRemoving:Connect(function(player)
SaveData(player)
data[player.UserId] = nil
end)
game:BindToClose(function()
if not RunService:IsStudio() then
for index, player in pairs(Players:GetPlayers()) do
task.spawn(function()
SaveData(player)
end)
end
else
print("Shutting down inside studio")
end
end)
local function getItemStatus(player, itemName)
local playerData = data[player.UserId]
if table.find(playerData.SelectedTowers, itemName) then
return "Equipped"
elseif table.find(playerData.OwnedTowers, itemName) then
return "Owned"
else
return "For Sale"
end
end
Functions.InteractItem.OnServerInvoke = function(player, itemName, Unequip)
local shopItem = towers[itemName]
local playerData = data[player.UserId]
if shopItem and playerData then
local status = getItemStatus(player, itemName)
if Unequip then
if #playerData.SelectedTowers > 1 then
local towerToRemove = table.find(playerData.SelectedTowers, itemName)
table.remove(playerData.SelectedTowers, towerToRemove)
end
else
if status == "For Sale" and shopItem.Price <= playerData.Bucks then
-- purchase
playerData.Bucks -= shopItem.Price
table.insert(playerData.OwnedTowers, shopItem.Name)
elseif status == "Owned" then
-- equip the tower
table.insert(playerData.SelectedTowers, shopItem.Name)
if #playerData.SelectedTowers > MAX_SELECTED_TOWERS then
table.remove(playerData.SelectedTowers, 1)
end
elseif status == "Equipped" then
-- unselect the tower (if more than 1 selected)
if #playerData.SelectedTowers > 1 then
local towerToRemove = table.find(playerData.SelectedTowers, itemName)
table.remove(playerData.SelectedTowers, towerToRemove)
end
end
end
return playerData
else
warn("Tower/player data does not exist")
end
return false
end
Functions.GetData.OnServerInvoke = function(player)
return data[player.UserId]
end
Events.SetBucks.Event:Connect(function(player, bucks)
data[player.UserId].Bucks += bucks
end)
Events.RewardTower.Event:Connect(function(player, tower)
if tower and player ~= nil then
local OwnedTable = data[player.UserId].OwnedTowers
table.insert(OwnedTable, tower)
end
end)
(if you have any questions about the scripts for my game, go ahead and ask)