Issues - Trying to change textlabel value

Hello! I would like to change the value of the TextLabel, but it’s not working, and I’m not sure what might be wrong. Any ideas on what could be the issue?

image

script:

-- Services
local Players = game:GetService("Players")

-- Get the TeamsModule
local TeamsModule = game.ReplicatedStorage:FindFirstChild("TeamsModuleScript")

-- Check if the module exists
if TeamsModule then
	local team1, team2 = require(TeamsModule).new()

	local redTeamLabel = script.Parent["Green counter"].Text
	local blueTeamLabel = script.Parent["Red counter"].Text

	local function updatePlayerCountLabels()
		redTeamLabel.Text = #team1.players .. "/3"
		blueTeamLabel.Text = #team2.players .. "/3"
	end

	local function onPlayerJoin(player)
		print(player.Name .. " joined the game")

		if #team1.players <= #team2.players then
			team1:addPlayer(player)
		else
			team2:addPlayer(player)
		end

		updatePlayerCountLabels()
	end

	Players.PlayerAdded:Connect(onPlayerJoin)
else
	warn("TeamsModule not found.")
end

modulescript:

local TeamsModule = {}

function TeamsModule.new()
	local team1 = {
		name = "Blue Team",
		players = {}, -- You can add more properties as needed
		maxPlayers = 3 -- Set the maximum number of players for Team 1
	}

	local team2 = {
		name = "Red Team",
		players = {}, -- You can add more properties as needed
		maxPlayers = 3 -- Set the maximum number of players for Team 2
	}

	return team1, team2
end

return TeamsModule
1 Like

Hi there!
After a bit of reading, I see that you’re not changing the “Text” attribute of the team labels correctly
You’re defining them correctly:

local redTeamLabel = script.Parent["Green counter"].Text
local blueTeamLabel = script.Parent["Red counter"].Text

Although you’re then not updating them correctly:

redTeamLabel.Text = #team1.players .. "/3"
blueTeamLabel.Text = #team2.players .. "/3"

Essentially you’re trying to change the “Text” value of the “Text” value, so this is what the script understands:

script.Parent["Green counter"].Text.Text = #team1.players .. "/3"
script.Parent["Red counter"].Text.Text = #team2.players .. "/3"

I hope this makes sense, English is not my main language and this is a bit hard to explain.

Although here’s how I would write the code:

-- Services
local Players = game:GetService("Players")

-- Get the TeamsModule
local TeamsModule = game.ReplicatedStorage:FindFirstChild("TeamsModuleScript")

-- Check if the module exists
if TeamsModule then
	local team1, team2 = require(TeamsModule).new()

	local redTeamLabel = script.Parent["Green counter"]
	local blueTeamLabel = script.Parent["Red counter"]

	local function updatePlayerCountLabels()
		redTeamLabel.Text = #team1.players .. "/3"
		blueTeamLabel.Text = #team2.players .. "/3"
	end

	local function onPlayerJoin(player)
		print(player.Name .. " joined the game")

		if #team1.players <= #team2.players then
			team1:addPlayer(player)
		else
			team2:addPlayer(player)
		end

		updatePlayerCountLabels()
	end

	Players.PlayerAdded:Connect(onPlayerJoin)
else
	warn("TeamsModule not found.")
end

Hope this helps!

2 Likes

Hello! Thank you very much for your comments, the truth is that I discovered that the error was because the information from the server to the client was not being updated and what I did was this (since I had an auto assignment in which my module script was occupying, so I updated the days from there to my client and it worked! :smiley: I’ll let you solve it, thank you very much!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playerAddedEvent = ReplicatedStorage:WaitForChild("PlayerAddedEvent")

local blueTeamLabel = script.Parent["blueCounter"].Text
local redTeamLabel = script.Parent["redCounter"].Text

playerAddedEvent.OnClientEvent:Connect(function(team1PlayerCount, team2PlayerCount)
	-- Update your UI or perform any other actions based on the player counts received from the server
	print("Team 1 Players: " .. team1PlayerCount)
	print("Team 2 Players: " .. team2PlayerCount)
	
	task.wait(.2)
	
	blueTeamLabel.Text = team1PlayerCount.."/3"
	redTeamLabel.Text = team2PlayerCount.."/3"
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.