Script can't detect team change?

Hi, I’m trying to script a small terminal system that detects if a player is part of a certain team, and depending on the output a different BoolValue will be activated, as well as a RemoteEvent.

main = script.Parent
event = game.ReplicatedStorage.Barupdate

while script.Win.Value == true do
	wait(1)
	if main.UEF.Value == true and main.Other.Value == false then
		script.UEFCount.Value = script.UEFCount.Value + 1
		event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
	elseif main.UEF.Value == true and main.Other.Value == true then
		print("contested!!")
		event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
	elseif main.UEF.Value == false and main.Other.Value == true then
		script.OtherCount.Value = script.OtherCount.Value + 1
		event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
	elseif main.UEF.Value == false and main.Other.Value == false then
		print("not taken by anyone!")
		event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
	else
		print("something else is happening!")
	end
end

Script works as intended when the player is originally set as a certain team, but if a player’s team is changed after the script is active, the script still outputs the player’s team as the one before the change.

1 Like

Try this:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local event = ReplicatedStorage.Barupdate
local main = script.Parent

--//Functions
local function CheckValue()
	while script.Win.Value do
		task.wait(1)

		if main.UEF.Value and not main.Other.Value then
			script.UEFCount.Value += 1
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		elseif main.UEF.Value and main.Other.Value then
			print("contested!!")
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		elseif not main.UEF.Value and main.Other.Value then
			script.OtherCount.Value += 1
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		elseif not main.UEF.Value and not main.Other.Value then
			print("not taken by anyone!")
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		else
			print("something else is happening!")
		end
	end
end

script.Win.Changed:Connect(function()
	CheckValue()
end)

CheckValue()

Is there a specific reason for using ReplicatedStorage service? It would save a couple lines if I just typed

local x = game.ReplicatedStorage.x 

instead of

ReplicatedStorage = game:GetService("ReplicatedStorage")
local x = ReplicatedStorage.x

It’s just the standard. If you’re using game.ReplicatedStorage, and the service is named something else, then your script will error. It’s also more performant to use :GetService instead of indexing from game.

Also, you should adding a local at the start, because there’s no need for it to be a global variable, which takes up more memory and takes more time to access for the script.

Thanks for clarifying, but the script changes didn’t fix the issue.

Could you show your client code for the remote events?

main = script.Parent

game.ReplicatedStorage.Barupdate.OnClientEvent:Connect(function(UEF, Raid, Req, won)
	if won == "UEF" then
		main.Parent:Destroy()
	elseif won == "Raid" then
		main.Parent:Destroy()
	else
		main.UEFTimer.Text = (tostring(UEF).."/"..tostring(Req))
		main.RaiderTimer.Text = (tostring(Raid).."/"..tostring(Req))
		if UEF == 0 then
			main.UEFBar.prog.Size = UDim2.new(0,0,1,0)
		elseif UEF == nil then
			print("finished!")
		else
			main.UEFBar.prog.Size = UDim2.new(UEF/Req,0,1,0)
		end
		if Raid == 0 then
			main.RaidBar.prog.Size = UDim2.new(0,0,1,0)
		elseif Raid == nil then
			print("finished!")
		else
			main.RaidBar.prog.Size = UDim2.new(Raid/Req,0,1,0)
		end
	end
end)

won string sent through RemoteEvent is used in a separate script that checks if the win requirement was met for either side

How do you get the UEFCount value then?

UEF in LocalScript = script.UEFCount.Value from server script
Raid = script.OtherCount.Value
Req is a seperate NumberValue that tells the client what the win requirement is.

Okay, I think I know the problem. It’s because you aren’t decreasing the team count when there’s no one contesting the point.

New code:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local event = ReplicatedStorage.Barupdate
local main = script.Parent

--//Functions
local function CheckValue()
	while script.Win.Value do
		task.wait(1)

		if main.UEF.Value and not main.Other.Value then
			script.UEFCount.Value += 1
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		elseif main.UEF.Value and main.Other.Value then
			print("contested!!")
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		elseif not main.UEF.Value and main.Other.Value then
			script.OtherCount.Value += 1
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		elseif not main.UEF.Value and not main.Other.Value then
			script.UEFCount.Value = 0 
			script.OtherCount.Value = 0
			
			print("not taken by anyone!")
			event:FireAllClients(script.UEFCount.Value, script.OtherCount.Value, script.WinReq.timereq.Value)
		else
			print("something else is happening!")
		end
	end
end

script.Win.Changed:Connect(function()
	CheckValue()
end)

CheckValue()

I’m starting to think the issue might not be with the counting script, but with the script parented to it, which chooses which team is holding the terminal

Players = game:GetService("Players")
Term = script.Parent

Term.Touched:Connect(function(par)
	if par.Name == "Handle" then
		plr = Players:GetPlayerFromCharacter(par.Parent.Parent)
	else
		plr = Players:GetPlayerFromCharacter(par.Parent)
	end
	if plr.Team == game.Teams.UEF or plr.Team == game.Teams.Allies then
		script.UEF.Value = true
		print(plr.Team)
	elseif plr.Team == game.Teams.Raiders then
		script.Other.Value = true
	end
end)

Term.TouchEnded:Connect(function(par)
	if par.Name == "Handle" then
		plr = Players:GetPlayerFromCharacter(par.Parent.Parent)
	else
		plr = Players:GetPlayerFromCharacter(par.Parent)
	end
	if plr.Team == game.Teams.UEF or plr.Team == game.Teams.Allies then
		script.UEF.Value = false
	elseif plr.Team == game.Teams.Raiders then
		script.Other.Value = false
	end
end)

(and here’s how the entire thing is laid out if it helps)
image