im making a tower defense game, but i cant get loadouts to work.
ive heard that datastores are shared between places.
my game has a main place, and a secondary place. both are in the same game, but the secondary place cant get the data. neither do i know how to make the loadout get sent by the elevators. which teleport the players.
here is the code for the elevators.
local players = {}
local functions = {}
local elevator = script.Parent.ElevatorPart
local tweenservice = game:GetService("TweenService")
local teleportservice = game:GetService("TeleportService")
local Time = script.Parent.Gui.SurfaceGui.Main.Countdown.Time.Value
local leaveremote = game.ReplicatedStorage.LeaveElevator
local db = false
local ismoving
local chosenmap = "Crossroads"
local module = require(game.ReplicatedStorage.Data)
leaveremote.OnServerEvent:Connect(function(player)
for i,v in ipairs(players) do
if v == player then
table.remove(players,i)
print(players)
end
end
player.Character.HumanoidRootPart.CFrame = script.Parent.ExitPos.CFrame
player.PlayerGui.ScreenGui.TextButton.Visible = false
end)
script.Parent.Enter.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and not db then
db = true
table.insert(players,1,plr)
script.Parent.Gui.SurfaceGui.Main.Players.Text = "Players: "..tostring(#players)
plr.PlayerGui.ScreenGui.TextButton.Visible = true
print(players)
wait(0.5)
db = false
end
end)
function functions.move()
local tinfo = TweenInfo.new(2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,true,1)
local goal = {
Position = Vector3.new(elevator.Position.X, 3, elevator.Position.Z)
}
local movetween = tweenservice:Create(elevator,tinfo,goal)
movetween:Play()
ismoving = true
end
function functions.teleport()
local teleportoptions = Instance.new("TeleportOptions")
teleportoptions:SetTeleportData(chosenmap)
local code = teleportservice:ReserveServer(8739888984)
teleportservice:TeleportToPrivateServer(8739888984,code,players,"SpawnLocation",teleportoptions)
code = teleportservice:ReserveServer(8739888984)
end
script.Parent.Teleport.Touched:Connect(functions.teleport)
while true do
script.Parent.Gui.SurfaceGui.Main.Map.Image = module.MapImages[chosenmap]["ID"]
script.Parent.Gui.SurfaceGui.Main.Map.MapName.Text = chosenmap
if Time ~= 0 and ismoving ~= true and #players ~= 0 then
Time = Time - 1
script.Parent.Gui.SurfaceGui.Main.Countdown.Text = Time
elseif Time == 0 then
functions.move()
script.Parent.Gui.SurfaceGui.Main.Countdown.Text = Time
Time = 10
ismoving = false
end
wait(1)
end
here is the code for the server, if your interested:
local players = game.Players
local towerdatastore = game:GetService("DataStoreService"):GetDataStore("TowerInventory")
function save(Player)
local saveData = {}
for i, v in pairs(Player:WaitForChild("TowerInventory"):GetChildren())do
saveData[#saveData + 1] = v.Name
end
towerdatastore:SetAsync(Player.UserId, saveData)
end
players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder")
folder.Name = "TowerInventory"
folder.Parent = plr
local succes, errorm = pcall(function()
local load = towerdatastore:GetAsync(plr.UserId)
if load then
for i,v in pairs(load) do
local x = Instance.new("IntValue", folder)
x.Name = v
end
elseif load == nil then
local c = Instance.new("IntValue",folder)
c.Name = "Gunner"
local g = Instance.new("IntValue",folder)
g.Name = "Sniper"
end
end)
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetChildren()) do
save(v)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if not game:GetService("RunService"):IsStudio() then
save(player)
end
end)