So, I have this piece of code that weirds me out a lot. I am trying to make a turn-based combat system, and I am stuck on the part of making cycle.
script.CurrentTurn.Value = AllTurnsTotal.Value
--
local function TurnCheck()
print("Check turning!")
local OldTurnNum = script.CurrentTurn.Value
for i, Participant in ipairs (AllParticipants) do
if Participant.TurnOrder.Value == script.CurrentTurn.Value then
print(Participant.Name.."'s Turn!")
local Player = game.Players:GetPlayerFromCharacter(Participant)
if Participant:FindFirstChild("IsAPlayer") ~= nil then
Player.PlayerGui.CombatUI.Enabled = true
wait(15)
if script.CurrentTurn.Value == OldTurnNum then
Player.PlayerGui.CombatUI.Enabled = false
local NT = Instance.new("BoolValue")
NT.Name = "NextTurn"
NT.Value = true
NT.Parent = script
end
elseif Participant:FindFirstChild("IsANPC") ~= nil then
local AbilitiesFolder = Participant:FindFirstChild("AbilitiesFolder")
if AbilitiesFolder ~= nil then
local IIM = Instance.new("BoolValue")
IIM.Name = "IsInMove"
IIM.Parent = Participant
IIM.Value = true
repeat wait(0.2) until Participant:FindFirstChild("IsInMove") == nil
local NT = Instance.new("BoolValue")
NT.Name = "NextTurn"
NT.Value = true
NT.Parent = script
else
wait(0.5)
local NT = Instance.new("BoolValue")
NT.Name = "NextTurn"
NT.Value = true
NT.Parent = script
end
end
end
end
end
--
TurnCheck()
script.CurrentTurn.Changed:Connect(function()
print("Someone added!")
TurnCheck()
end)
My issue is that it works at regular call before “script.CurrentTurn.Changed” function, but when CurrentTurn changes (it’s a number value, just in case) - it just prints out “Someone added!” yet function has absolutely no functionality - it does not even print “Check turning!”.
Could anyone help? I am kinda new to using “functions” and maybe I didn’t get something right, but it confuses me a lot and I am stuck for around 30 minutes already.
It changes - when I make my turn, it changes to what’s required (Player’s turn “number” is 2, NPC is 1, at start “CurrentTurn” is 2, after my attack it turns into 1)
It detects that it changes because it prints “Someone added!” in console, yet anything from function does not happen as it doesn’t even print “Check turning!”
Omitting all code except the print statement in TurnCheck(), it runs normally for me. Are you sure you’re not overwriting the turncheck function somewhere?
Not really, this is the only function. There is a separate script that removes “NextTurn” that is created by this script and reduces “CurrentTurn” value by 1.
Both are server scripts. This is done since it didn’t want to work with “ChildRemoved” function when “NextTurn” are removed in the script that I sent on original post.
Maybe there are any other ways to re-write this all a little?
All what I need is reducing “CurrentTurn” value by 1 every time it is a turn end (players do it trough their attacking and stuff, NPCs are written there), and a way to detect when it gets reduced by 1.
I will try figuring something out on my own, but help would be very appreciated.
I don’t see why it wouldn’t print “Check turning!” though.
You can try a bindable event or bindable function and fire/invoke that whenever you reduce current turn, but once again theres no reason TurnCheck() shouldn’t run after the print statement unless there were some other factor at play