Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

The code in the description results in a block that is not changing colors why does this happen?

This is in Roblox Lua, by the way. The result is a block that doesn't change color once I touch it. This text is to add characters to my description that isn't code so I can post it.

local player = game.Players.LocalPlayer local part = Instance.new("Part") part.Parent = game.Workspace local maxRGBValue = 500 local minRGBValue = 0 local r = 0 local g = 0 local b = 0 part.Touched:Connect(function(hit) if hit.Parent == player then while true do while r + g + b < maxRGBValue do wait(.03) r = r + 1 g = g + 1 b = b + 1 part.Color = Color3.fromRGB(r,g,b) end while r + g + b > minRGBValue do wait(.03) r = r - 1 g = g - 1 b = b - 1 part.Color = Color3.fromRGB(r,g,b) end end end end)


1 answer

Your if statement is the problem, here is how you fix it:

local player = game.Players.LocalPlayer
local part = Instance.new("Part")
part.Parent = workspace
local maxRGBValue = 500
local minRGBValue = 0
local r = 0
local g = 0
local b = 0

part.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		while true do wait()
			while r+g+b < maxRGBValue do wait(0.3)
				r = r + 1
				g = g + 1
				b = b + 1
				part.Color = Color3.fromRGB(r,g,b) 
			end
			while r + g + b > minRGBValue do wait(.03) 
				r = r -1 
				g = g - 1
				b = b - 1
				part.Color = Color3.fromRGB(r,g,b)
			end
		end
	end
end)

Honestly, you also could have done things like

if tostring(hit.Parent) == tostring(player) then
--or
if hit.Parent.Name == player.Name then

But let's just say for whatever reason you have another Instance and its parents name is the same as the player, so I suggest using the one I did first to make sure it is a player.

This discussion is preserved as it was. New answers, comments, and votes are closed.