I’m making a tower defense game I’m trying to detect if a player has already bought in a tower
I’m trying not to make it where I mainly have to put in each Tower
local datastoreservice = game:GetService("DataStoreService")
local mystore = datastoreservice:GetDataStore("mydatastore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Loadout = Instance.new("Folder",player)
Loadout.Name = "Loadout"
local LVFolder = Instance.new("Folder",player)
LVFolder.Name = "LVFolder"
local TowerData = Instance.new("Folder",player)
TowerData.Name = "TowerData"
local TowerDataTal = {}
local Gold = Instance.new("IntValue",leaderstats)
Gold.Name = "Gold"
Gold.Value = 0
local LV = Instance.new("IntValue",leaderstats)
LV.Name = "LV"
LV.Value = 0
local current = Instance.new("IntValue",LVFolder)
current.Name = "current"
current.Value = 0
local goal = Instance.new("IntValue",LVFolder)
goal.Name = "goal"
goal.Value = 0
local s1 = Instance.new("StringValue",Loadout)
s1.Name = "s1"
local s2 = Instance.new("StringValue",Loadout)
s2.Name = "s2"
local s3 = Instance.new("StringValue",Loadout)
s3.Name = "s3"
local s4 = Instance.new("StringValue",Loadout)
s4.Name = "s4"
local s5 = Instance.new("StringValue",Loadout)
s5.Name = "s5"
local playerid = "Player_" .. player.UserId
local data = mystore:GetAsync(playerid)
if data then
LV.Value = 0
goal.Value = 100
Gold.Value = data['Gold']
current.Value = data['current']
goal.Value = data['goal']
LV.Value = data['LV']
s1.Value = data['s1']
s2.Value = data['s2']
s3.Value = data['s3']
s4.Value = data['s4']
s5.Value = data['s5']
else
LV.Value = 0
current.Value = 0
goal.Value = 100
Gold.Value = 0
end
if current.Value >= goal.Value then
LV.Value = LV.Value + 1
if current.Value > goal.Value then
current.Value -= goal.Value
else
current.Value = 0
end
goal.Value = goal.Value + 100
end
current.Changed:Connect(function()
if current.Value >= goal.Value then
LV.Value = LV.Value + 1
if current.Value > goal.Value then
current.Value -= goal.Value
else
current.Value = 0
end
goal.Value = goal.Value + 100
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local datatobesaved = {
Gold = player.leaderstats.Gold.Value;
goal = player.LVFolder.goal.Value;
LV = player.leaderstats.LV.Value;
current = player.LVFolder.current.Value;
s1 = player.Loadout.s1.Value;
s2 = player.Loadout.s2.Value;
s3 = player.Loadout.s3.Value;
s4 = player.Loadout.s4.Value;
s5 = player.Loadout.s5.Value;
}
local playerid = "Player_" .. player.UserId
local success, err = pcall(function()
mystore:SetAsync(playerid,datatobesaved)
end)
if success then
print("data saved!")
else
print("data faield to save!")
end
end)
here’s my code