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

Brick colour change script isn't working?

My script to change the colour of a brick isn't working?

game:WaitForChild("Players").PlayerAdded:Connect(function(plr)
	local stage = plr:WaitForChild("leaderstats"):WaitForChild("Stage")
	stage.Changed:Connect(function()
	if stage.Value >= 1 or script.Parent.Touched then 
		script.Parent.CanCollide = false
		game.Workspace.Walls.Wall4.CanCollide = true
		game.Workspace.Walls.Wall4.BrickColor = BrickColor.new("Really red")
	end
	end)
	end)
	

3 answers

✓ Accepted answer

Events cannot be used as conditions, nor do they act like conditions.

Use a separate function for Touched, such as:

game.Players.PlayerAdded:Connect(function()
	script.Parent.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			if workspace:FindFirstChild("Wall4", true) then
				local wall = workspace:FindFirstChild("Wall4", true)
				wall.CanCollide = true
				wall.BrickColor = BrickColor.new("Really red")
			-- Your other function would go here
			end
		end
	end)
end)

Let me explain why you can't use events in conditional statements.

Events are RBXScriptSignals. RBXScriptSignals are signals that act like triggers for given functions; when they fire, a function is usually associated with it (unless you use Wait()). However, the main reason why they can't be used as conditions is because RBXScriptSignals don't return any values, they simply pass them. The difference is that passing a value means giving it to a function, and returning a value means that a function passes its own value to wherever it is called.

Uh maybe, just maybe this script will help you, the scripts on top is incorrect.

game:WaitForChild("Players").PlayerAdded:Connect(function(plr)
	    local stage = plr.leaderstats.Stage
	    stage.Changed:Connect(function()
	    if stage.Value >= 1 or script.Parent.Touched then
	        script.Parent.CanCollide = false
	        game.Workspace.Walls.Wall4.CanCollide = true
	        game.Workspace.Walls.Wall4.BrickColor = BrickColor.new("Really red")
	    end
	    end)
	    end)

so i can't really read the whole thing but:

game:WaitForChild("Players").PlayerAdded:Connect(function(plr)
    local stage = plr:WaitForChild("leaderstats"):WaitForChild("Stage")
    stage.Changed:Connect(function()
    if stage.Value >= 1 or script.Parent.Touched then 
        script.Parent.CanCollide = false
        game.Workspace.Walls.Wall4.CanCollide = true
        game.Workspace.Walls.Wall4.BrickColor = BrickColor."Really red" --- that might be the problem,you should use . random
    end
    end)
    end)

---make sure its anchored too.

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