Hey yall so I’m learning how to create a turn based game and I think I’m on the right track atm although I’ve encountered an issue with my code basically I have 2 functions 1 of them creates a new battle and another one joins a battle.
local function createBattle(player,enemy)
if deb == false then
deb = true
IntitateBattle()
activeBattles[player.Name.."'s Battle"] = {
playersInBattle = {},
enemiesInBattle = {},
}
local battleArea = nil
battleArea = servStorage["Battle Area"]:Clone()
local hmrPos = player.Character.HumanoidRootPart.Position
battleArea:SetPrimaryPartCFrame(CFrame.new(hmrPos.X,hmrPos.Y-3,hmrPos.Z)*CFrame.Angles(0,0,math.rad(90)))
battleArea.Parent = workspace
battleArea.Name = player.Name.."'s Battle"
repeat wait() until battleArea ~= nil
joinBattle(battleArea.Name,player)
joinBattle(battleArea.Name,enemy)
wait(3)
test:FireClient(player,activeBattles[player.Name.."'s Battle"],battleArea,deck,inhand)
deb = false
end
end
local function joinBattle(battleToJoin,v1) --not sure if having 1 arg for equaling multiple things is good or not
print(battleToJoin,v1)
if game.Players:FindFirstChild(v1.Name) then
print("player")
local spots = workspace[battleToJoin]["Friendly Spots"]:GetChildren()
if #spots ~= 0 then
print("Available Spots: "..#spots)
if not v1:FindFirstChild("In Battle") then
local inBattle = Instance.new("BoolValue")
inBattle.Name = "In Battle"
inBattle.Parent = v1.Character
end
local selectedSpot = spots[math.random(1,#spots)]
local selectedSpotPos = selectedSpot.Position
disableControls:FireClient(v1)
v1.Character.Humanoid:MoveTo(selectedSpotPos)
selectedSpot.Parent = workspace[battleToJoin] --so it doesn't get picked again
local playersInBattle = activeBattles[battleToJoin].playersInBattle
if #playersInBattle < 4 then
table.insert(playersInBattle,v1.Name)
print("player added")
end
end
elseif game.Workspace.Enemies:FindFirstChild(v1.Name) then
print("enemy")
local spots = workspace[battleToJoin]["Enemy Spots"]:GetChildren()
if #spots ~= 0 then
if not v1:FindFirstChild("In Battle") then
local inBattle = Instance.new("BoolValue")
inBattle.Name = "In Battle"
inBattle.Parent = v1
end
local selectedSpot = spots[math.random(1,#spots)]
local selectedSpotPos = selectedSpot.Position
v1.Humanoid:MoveTo(selectedSpotPos)
selectedSpot.Parent = workspace[battleToJoin] --so it doesn't get picked again
local enemiesInBattle = activeBattles[battleToJoin].enemiesInBattle
if #enemiesInBattle < 4 then
table.insert(enemiesInBattle,v1.Name)
print("enemy added")
end
end
end
end
So it’s kind of long sorry but it works, but I send the table in the dictionary to the client so that I’m able to check on the client what characters or enemies are in the battle and so I can check what the player can target although if a new player or enemy were to join I don’t have a thing to send an updated version
Edit: Actually now that I think about it is it possible that I could make a remote function that just gets the table? I can make a remote function and send to the server the battle I need to access in the dictonary and then it returns the yeah.