Touching the Brick Only Works Once

So basically I’m trying making it so whenever you touch a part (Albert’s Head) it will run everything in it.

Code:
Albert = script.Parent
Event = script.Parent.Event.Value

Albert.Touched:connect(function()
if Event == 1 then
script.Parent.Parent.Parent.Baseplate.BrickColor = BrickColor.new(“Black”)
elseif Event == 2 then
script.Parent.Parent.Parent.Baseplate.BrickColor = BrickColor.new(“Really red”)
elseif Event == 3 then
script.Parent.Parent.Parent.Baseplate.BrickColor = BrickColor.new(“Dark indigo”)
end
end)

The issue is it will work perfectly the first time you touch his head, it will change the Baseplate color but if you touch it again it won’t do anything and I need it to work every time you touch it.

Perhaps, the value is greater then 3, which stops the script from firing the code inside the if statement?

Also, :connect is deprecated, so use :Connect.

Also, localise your variables :slight_smile:

Is the event value increased? Just to be sure

Check that the hit is a player, using get player from character, from the hit.
That way it will happen only when a player does that, and as dibblydubblydoo said, you need to localise your variables, write local before every variable, like that:

local num = 1 --That way
num2 = 1 --Not that way

And get the hit from the function, llke this:
for example:

script.Parent.Touched:Connect(function(hit) --Do you see that hit? it's the part that touched that brick.
   print(hit) --To prove it, it will print the part that touched name.
end)

:GetPlayerFromCharacter()
Touched Event
Edit: And also, you are getting the value of the event, if it changes, nothing will happen, get the object instead, and write .Value when you check if it’s 1/2/3
Like this:

local event = workspace.IntValue --For example, I'll get an int value, not the value itself, but the instance.
script.Parent.Touched:Connect(function(hit)
   if(event.Value == 1) --I added .Value because the variable holds the instance, and not the value before it was touched.
   end
end)

You can also do that like this, and get the value every time it is touched, and not before:

script.Parent.Touched:Connect(function(hit)
   local event = workspace.IntValue.Value --Here I get the value every time it is touched.
   if(event == 1) 
   print("Value is equal to 1")
   end
end)

“Event”, will always equal the number set before you connect the touch event, as you’re grabbing

Event = script.Parent.Event.Value

Move this into the touch event.

It also looks like you’re not incrementing it either, so don’t forget to add to Event.Value every touch.

1 Like

When you call an Object.Value it’s value is stored as an integer without a reference to the Object.
That’s why it did not change.
Here’s a simple example on a method to achieve what you wanted to achieve.

local Albert = workspace:WaitForChild("Albert")
local Baseplate = workspace.Baseplate
local event = 1
local debounceDelay = 0.5
local connection

local colors = {
    BrickColor.new("Black"),
    BrickColor.new("Really red"),
    BrickColor.new("Dark indigo")    
}

local function onTouched()
    local color = colors[event]
    if typeof(color) == "BrickColor" then
        Baseplate.BrickColor = color
    end
    if connection then
        connection:Disconnect()
    end
	event = event % #colors + 1
	wait(debounceDelay)
    connection = Albert.Touched:Connect(onTouched)
end

connection = Albert.Touched:Connect(onTouched)

Here’s a preview:

1 Like