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

Function not changing brick's material in "if" statement?

This function seems to just skip over the if statement / ignore it and goes right to the elseif, even if the if statement is true...

	e.Touched:connect(function(hit)
if  hit.Anchored == true and hit.CanCollide == true and hit.Parent ~= char then
	if hit.Material == "Grass" then
					e.Material = "Slate"
					e.BrickColor = BrickColor.new("Brown")
					
	elseif hit.Material ~= "Grass" then
					 	e.Material = hit.Material
					e.Color = hit.Color
				wait()
				end
end
end)

2 comments

0

which if statement does it skip, it look like the if hit.Anchored... has two spaces between if and hit i dont know if that matters though.

0

Whitespace is not the error, Lua ignores this.

1 answer

✓ Accepted answer

###This is happening because the value of Material is an Enum. If you wanted to check if the material was grass, you'd have to say if hit.Material == Enum.Material.Grass.

e.Touched:Connect(function(hit)
    if hit.Anchored and hit.CanCollide and hit.Parent ~= char then
        if hit.Material == Enum.Material.Grass then
            e.Material = Enum.Material.Slate
            e.BrickColor = BrickColor.new("Brown")
        else
            e.Material = hit.Material
            e.Color = hit.Color
            wait()
        end
    end
end)

###It's always better to assign Material as an enum as in conditionals it's required.

###Don't say that the if statement was true, because it was false.

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