My code wont work for some reason, but it has worked in the past!

My code doesn’t work.

  1. What do you want to achieve? My code, it is supposed to add to a value when a child of an object is touched, and subtract another value.
  2. What is the issue? It doesn’t change the values.
  3. What solutions have you tried so far? I tried another way to change the value, but it didn’t work.

My code is below.

local cubes = script.Parent.Paintableparts:GetChildren()
local numUnpainted = game.ReplicatedStorage.PercentageValues.NumUnpainted
local numRed = game.ReplicatedStorage.PercentageValues.NumRed
local numBlue = game.ReplicatedStorage.PercentageValues.NumBlue

for i,v in pairs(cubes) do
	v.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

		if plr then
			if v.BrickColor == BrickColor.new("Medium stone grey") then
				if plr.TeamColor == BrickColor.new("Bright red") then
					numUnpainted.Value = numUnpainted.Value - 1
					numRed.Value = numRed.Value + 1
				elseif plr.TeamColor == BrickColor.new("Bright blue") then
					numUnpainted.Value = numUnpainted.Value - 1
					numBlue.Value = numBlue.Value + 1
				end
			end
			if v.BrickColor == BrickColor.new("Bright red") then
				if plr.TeamColor == BrickColor.new("Bright blue") then
					numRed.Value = numRed.Value - 1
					numBlue.Value = numBlue.Value + 1
				end
			elseif v.BrickColor == BrickColor.new("Bright blue") then
				if plr.TeamColor == BrickColor.new("Bright red") then
					numBlue.Value = numBlue.Value - 1
					numRed.Value = numRed.Value + 1
				end
			end
		end
	end)
end

Please help!
Also, the imagesbelow will have the explorer.
Annotation 2021-10-24 124319

What’s the issue? Any further details such as errors in console?

1 Like


Nothing (those prints are from another script)

local cubes = script.Parent.Paintableparts:GetChildren()
local numUnpainted = game.ReplicatedStorage.PercentageValues.NumUnpainted
local numRed = game.ReplicatedStorage.PercentageValues.NumRed
local numBlue = game.ReplicatedStorage.PercentageValues.NumBlue

for i,v in pairs(cubes) do
	v.Touched:Connect(function(hit)
		if v.Parent:FindFirstChild("HumanoidRootPart") then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

			if plr then
				if v.BrickColor == BrickColor.new("Medium stone grey") then
					if plr.TeamColor == BrickColor.new("Bright red") then
						numUnpainted.Value = numUnpainted.Value - 1
						numRed.Value = numRed.Value + 1
					elseif plr.TeamColor == BrickColor.new("Bright blue") then
						numUnpainted.Value = numUnpainted.Value - 1
						numBlue.Value = numBlue.Value + 1
					end
				end
				if v.BrickColor == BrickColor.new("Bright red") then
					if plr.TeamColor == BrickColor.new("Bright blue") then
						numRed.Value = numRed.Value - 1
						numBlue.Value = numBlue.Value + 1
					end
				elseif v.BrickColor == BrickColor.new("Bright blue") then
					if plr.TeamColor == BrickColor.new("Bright red") then
						numBlue.Value = numBlue.Value - 1
						numRed.Value = numRed.Value + 1
					end
				end
			end
		end
	end)
end

You were lacking a check to make sure that the touching part (which caused the event to fire) belonged to a player.

hit will always evaluate to true, it changes nothing.

The check didn’t change if it worked or not… I tested.
By the way, v is the cube that is being touched.

It prevents the script from trying to use any parts parent to get a player from their character.