Help me on this event script

Hey does anyone know what is wrong with this script? I know it’s simple, but i’m a noob at scripting. I just started and I am trying to learn basic events.

image

1 Like
script.Parent.Touched:Connect(function(hit)
	script.Parent.BrickColor = BrickColor.new("New Yeller")
end)

It should be that, and not “hit” at the beginning, @adam7878g

EDIT:

function OnTouched()
    script.Parent.Brickcolor = Brickcolor.new("New Yeller")
end

script.Parent.Touched:Connect(OnTouched)
1 Like

You can’t make parameters have strings, I’m not entirely sure though but I have never seen parameters with strings. Also, you are trying to execute a function using hit() but you have not created one and finally you set the BrickColor of nothing to New Yeller, try using this code:

script.Parent.Touched:Connect(function(touched)  -- creates a function using the touched event

if touched then -- checks whether the player has touched the part
    script.Parent.BrickColor = BrickColor.new("New Yeller") -- changes the part's color
end

is touched the name of the function? and also when i want to execute the function do i do touched()

No, you don’t need to execute the function because the function will automatically execute whenever a player’s character touches the part so you don’t have to worry about that.

Touched is there so that we can check whether somebody has touched the part, I don’t think its neccessary though.

Parameters don’t take string values, you would just write function(hit)
What are you changing the brick color of? Unless BrickColor is specified as the brick color of a certain part I’m not sure it will do anything. Also you shouldn’t call a variable “BrickColor” if that is the case because it’s a keyword

Also I don’t think you can call hit() as a function. Hit is just the object that triggered the .Touched event, and the function is connected automatically when the event is fired.

ohh ok but i think i missed out the part where i have to write FindFirstChild (“Humanoid”)

Do you want to reset the character if the character touches the part?

no i want the part color to change to yellow

Then my script should work, I don’t understand what you mean by finding the humanoid, theres no need for you to find the humanoid.

local myTouch = true

script.Parent.Touched:Connect(function(hit)
    if myTouch then
        myTouch = false --cannot touch (cooldown)

        local character = hit:FindFirstAncestorOfClass("Model") --gets the player's character model
        local humanoid = character:FindFirstChild("Humanoid") --gets humanoid

        wait(1) --cooldown time

        myTouch = true --can touch again
    end
end)

thanks everyone it works nowwwww

if you want to change brickcolor:

local myTouch = true

script.Parent.Touched:Connect(function(hit)
    if myTouch then
        myTouch = false --cannot touch (cooldown)

        if hit.Parent:IsA("BasePart") then --makes sure the hit is a valid part
            hit.Parent.BrickColor = BrickColor.new("New Yeller")
        end

        wait(1) --cooldown time

        myTouch = true --can touch again
    end
end)

Just another way of doing it

Ah. I see the problems. When a Touched event fires, it returns what touched it. When you type script.Parent.Touched:Connect(function(<customvariable>), the custom variable is the item that touched the part. The custom variable may not be in quotation marks, as it is a variable, not a string. Variables can only be accessed out of quotation marks. Also, the hit() at the bottom is useless, as there is no function called hit() and you already have a way to trigger the function (script.Parent.Touched). At line 2, mytouch is not defined. If you want to see if it was a player that touched the object, you would need to use:

if hit.Parent:FindFirstChild("Humanoid") then 
	-- BrickColor command goes here
end

This checks the object to see if it is in a model with a Humanoid, if it is, it is safe to say that hit is part of a player. Basically, the command will only run if the object that touched it is a player

Also, BrickColor is not defined. You must say which object you want to change the color of. In this case, it would be script.Parent.BrickColor = BrickColor.new("New Yeller")

Here is a fixed version of your code:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.BrickColor = BrickColor.new("New Yeller")
	end
end)

It sounds complicated but it is very simple.

I think you made a mistake in the function , you made a string value there. So far i know that isn’t a thing