Issues with trying to apply a script into multiple parts

So, as the tittle says, i am trying to apply a script into multiple parts for a capture points system, however, i dont know why this is happening, the script is applyied into just one part, i have tryied some possible solutions but no one worked.

local module = {}

function module.GameMode1()
	local bluesInPoint = {}
	local redsInPoint = {}
	local redPoints = 1
	local bluePoints = 5
	local blueTeam = game.Teams.BlueTeam
	local redTeam = game.Teams.RedTeam

	local points
	
	for i,v in pairs(workspace.Map1:GetChildren()) do
		points = v
	end
	
	points.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if hit.Parent:FindFirstChild("Humanoid") then
			if player.Team == blueTeam then
				if table.find(bluesInPoint,player.UserId) then
					table.insert(bluesInPoint,player.UserId)
				end
				
			elseif player.Team == redTeam then
				if not table.find(redsInPoint,player.UserId) then
					table.insert(redsInPoint,player.UserId)
				end
			end
			

		end
	end)
	
	points.TouchEnded:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			
		if hit.Parent:FindFirstChild("Humanoid") then
			if player.Team == blueTeam then
				local index = table.find(bluesInPoint,player.UserId)
				if index then
					table.remove(bluesInPoint,table.find(bluesInPoint,player.UserId))
				end
			elseif player.Team == redTeam then
					local index = table.find(redsInPoint,player.UserId)
					if index then
						table.remove(redsInPoint,table.find(redsInPoint,player.UserId))
					end
				end
			end
		end)
	
	task.spawn(function()
		while true do
			local percentValue = points.Part.Part.percentValue
			local percentText = points.Part.Part.SurfaceGui.percent
		
			if #redsInPoint > #bluesInPoint then
				local points = (#redsInPoint - #bluesInPoint)
				percentValue.Value -= points
			end
			
			if percentValue.Value < 0 then
				percentValue.Value = 0
			end
			
			if #bluesInPoint > #redsInPoint then
				local points = (#bluesInPoint - #redsInPoint)
				percentValue.Value += points
			end
			
			if percentValue.Value > 100 then
				percentValue.Value = 100
			end
		
			percentText.Text = tostring(percentValue.Value)
			task.wait(2)
		end
	end)
	

	
	while task.wait() do
		points.Part.countBlue.Value = #bluesInPoint
		points.Part.SurfaceGui.blueCount.Text = points.Part.countBlue.Value
		points.Part.CountRed.Value = #redsInPoint
		points.Part.SurfaceGui.redCount.Text = points.Part.CountRed.Value
	end
end

return module

the local points is just one point, and all of them are identical.

image

here you loop through the entire map. then at every single index, you change points to a different instance. so your just going thrhough, changed it, and then at the end it takes the value of whatever the last thing in the table was.

basically, points is just 1 part. did you already know that? or are you trying to make it a table of all the points?

no, i didnt know that, and yes, i am trying to get all the points

You might as well use every object in Map1 then only the last object in Map1. (use all the points, not just 1)

Just put the 2 touch events in a loop, you aren’t looping through the children of Map1 properly.

i have tryied it but doesnt work

What does the output say? Check the output, so that you can identify errors.

1 Like

the output says just one point

1 Like

Yeah? Your code shows that you are using 1 point, the code I gave you allows you to loop through the children of Map1 (points) so that it can apply the touch events to all of them.

You also need to make sure that there isn’t only 1 child of Map1.

yes, i thought the same when i tryied it

i made the code a local function and i call it for every point but for some reason i dont understand, it doenst work too

Also, here you put “if a player user id is not in redsinpoint table then add it” but for the bluesinpoint, you are adding it to the table if it’s already in the table… You forgot to add a “not”

oh, yes, thanks, i think i erase that “not”

Also, this while loop should be somewhere else, preferably in the code which calls/requires the module, otherwise it won’t return at all, you haven’t even added a condition to the while loop so it will never return.

ok, but it is working perfectly

wait, i remove it and them the code works

Does the code work now when you remove the while loop?

1 Like

because this while loop causes somo weird bugs and i forgive them, thanks to make me remember that

as it seens, yes, it is working now

1 Like

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