One is a Localscript in my button, the other is a Serverscript in workspace, And the third thing is a remove event called “ButtonClickEvent”
When i playtest my game it ends up with printing that the total clicks is always “0 clicks” when the timer hits 0 seconds no matter how many times i press my button
Localscript in the button:
local button = script.Parent
local player = game.Players.LocalPlayer
local timerLabel = player:WaitForChild("PlayerGui"):WaitForChild("TugOfWar"):WaitForChild("Timer")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonClickEvent = ReplicatedStorage:WaitForChild("ButtonClickEvent")
local clicksInArea1 = 0
local clicksInArea2 = 0
-- Use FindFirstChild to safely get Area1 and Area2
local side1 = workspace.TugOfWar.SIDES.Side1
local side2 = workspace.TugOfWar.SIDES.Side2
local area1 = side1:FindFirstChild("Area1")
local area2 = side2:FindFirstChild("Area2")
-- Function to update button visibility based on the timer
local function updateButtonVisibility()
print("Current timer text:", timerLabel.Text) -- Debugging timer value
if timerLabel.Text == "Starting..." then
print("Timer is in starting phase.")
button.Visible = false -- Keep the button hidden during this phase
return
end
local timerValue = tonumber(timerLabel.Text) -- Convert text to a number
if timerValue then
if timerValue >= 1 and timerValue <= 10 then
button.Visible = true -- Show the button when the timer is between 1 and 10
else
button.Visible = false -- Hide the button when the timer is not between 1 and 10
end
else
print("Timer is not numeric. Ignoring.")
button.Visible = false
end
end
-- Listen for changes to the timer's text to update button visibility
timerLabel:GetPropertyChangedSignal("Text"):Connect(updateButtonVisibility)
-- Track button clicks when the button is clicked
button.MouseButton1Click:Connect(function()
local timerValue = tonumber(timerLabel.Text)
if timerValue and timerValue >= 1 and timerValue <= 10 then
-- Check if the player is in Area1 or Area2 and count the clicks accordingly
local character = player.Character
if character and area1 and area2 then
local position = character.HumanoidRootPart.Position
-- Check if player is in Area1
if (math.abs(position.X - area1.Position.X) <= area1.Size.X / 2) and
(math.abs(position.Y - area1.Position.Y) <= area1.Size.Y / 2) and
(math.abs(position.Z - area1.Position.Z) <= area1.Size.Z / 2) then
clicksInArea1 = clicksInArea1 + 1
end
-- Check if player is in Area2
if (math.abs(position.X - area2.Position.X) <= area2.Size.X / 2) and
(math.abs(position.Y - area2.Position.Y) <= area2.Size.Y / 2) and
(math.abs(position.Z - area2.Position.Z) <= area2.Size.Z / 2) then
clicksInArea2 = clicksInArea2 + 1
end
end
end
end)
-- Function to print the total clicks when the timer reaches 0
local function printTotalClicks()
print("Area1 got", clicksInArea1, "clicks in total!")
print("Area2 got", clicksInArea2, "clicks in total!")
end
-- Call the function to print total clicks when timer reaches 0
timerLabel:GetPropertyChangedSignal("Text"):Connect(function()
if timerLabel.Text == "0" then
printTotalClicks() -- Print total clicks when the timer hits 0
end
end)
-- Initial visibility check
updateButtonVisibility()
Serverscript:
local GameArea = script.Parent
local side1 = workspace.TugOfWar.SIDES.Side1
local side2 = workspace.TugOfWar.SIDES.Side2
-- Use FindFirstChild to safely get Area1, Area2, DeathTeleport1, and DeathTeleport2
local area1 = side1:FindFirstChild("Area1")
local area2 = side2:FindFirstChild("Area2")
local DeathTeleport1 = side1:FindFirstChild("DeathTeleport1")
local DeathTeleport2 = side2:FindFirstChild("DeathTeleport2")
local playersToTeleport = {} -- List of players to teleport
local playersInGameArea = {} -- Track players currently in the GameArea
local gameStarted = false
local countdownRunning = false -- Prevent multiple countdowns
-- Function to divide and teleport players to sides
local function divideAndTeleportPlayers()
if #playersToTeleport == 0 then
print("No players to teleport!")
return
end
local group1, group2 = {}, {}
-- Divide players into two groups
for i, player in ipairs(playersToTeleport) do
if i % 2 == 1 then
table.insert(group1, player)
else
table.insert(group2, player)
end
end
-- Teleport group1 near Area1
for _, player in ipairs(group1) do
local character = player.Character
if character and area1 then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
-- Teleport to a position near Area1 (offset slightly from Area1's position)
humanoidRootPart.CFrame = area1.CFrame + Vector3.new(0, 5, 0) -- Adjust offset as needed
end
end
end
-- Teleport group2 near Area2
for _, player in ipairs(group2) do
local character = player.Character
if character and area2 then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
-- Teleport to a position near Area2 (offset slightly from Area2's position)
humanoidRootPart.CFrame = area2.CFrame + Vector3.new(0, 5, 0) -- Adjust offset as needed
end
end
end
print("Players have been divided and teleported!")
end
local function teleportLosingSide()
-- Ensure playersToTeleport is populated
if #playersToTeleport == 0 then
warn("No players to teleport!")
return
end
-- Ensure DeathTeleport1 and DeathTeleport2 are valid
if not DeathTeleport1 or not DeathTeleport2 then
warn("One or both death teleport parts are missing!")
return
end
local losingArea = nil
-- Determine the losing area (simple alternate)
if #playersInGameArea % 2 == 1 then
losingArea = "Side1"
else
losingArea = "Side2"
end
if losingArea then
local deathTeleport = (losingArea == "Side1") and DeathTeleport1 or DeathTeleport2
print("Losing area is:", losingArea)
for _, player in ipairs(playersToTeleport) do
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
print("Teleporting player:", player.Name, "to", deathTeleport.Name)
humanoidRootPart.CFrame = deathTeleport.CFrame
else
warn("HumanoidRootPart missing for player:", player.Name)
end
else
warn("Character missing for player:", player.Name)
end
end
else
print("It's a tie! No teleportation.")
end
end
-- Countdown timer for all players
local function startCountdownForAllPlayers()
if countdownRunning then return end -- Prevent multiple countdowns
countdownRunning = true
-- Function to update timers for players in the GameArea
local function updateTimersForAllPlayers(text, visible)
for _, player in ipairs(playersInGameArea) do
local playerGui = player:FindFirstChild("PlayerGui")
if playerGui then
local timerLabel = playerGui:WaitForChild("TugOfWar"):WaitForChild("Timer")
if timerLabel then
timerLabel.Visible = visible
timerLabel.Text = text
end
end
end
end
-- Wait 8 seconds before starting the countdown
updateTimersForAllPlayers("Waiting...", true)
task.wait(8)
-- Countdown logic
for i = 10, 0, -1 do
updateTimersForAllPlayers(tostring(i), true) -- Update timer for all players
task.wait(1) -- Wait 1 second
end
-- Hide the timer for all players and teleport losing side
updateTimersForAllPlayers("", false)
teleportLosingSide()
end
-- Function to teleport players to the losing side's death zone
-- Function to collect players currently in the GameArea
local function collectPlayers()
playersToTeleport = {} -- Reset the teleport list
for _, player in ipairs(playersInGameArea) do
if not table.find(playersToTeleport, player) then
table.insert(playersToTeleport, player)
end
end
end
-- Detect when a player touches GameArea
GameArea.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not gameStarted then
if not table.find(playersInGameArea, player) then
table.insert(playersInGameArea, player)
end
if not countdownRunning then -- Start the countdown only once
print("Game starting soon...")
task.delay(15, function()
if #playersInGameArea > 0 then -- Ensure players are still in the GameArea
collectPlayers() -- Collect all players to teleport
divideAndTeleportPlayers() -- Teleport players
startCountdownForAllPlayers() -- Start the countdown after teleportation
gameStarted = true
end
end)
end
end
end)