Help me out, I used somthing like this
function onTouched() script.Parent.Brickcolor = Brickcolor.new(Really green) end
If this is your entire code, there is nothing to make the function run. Functions only run when they are called, and since you never call it, it will never run.
If you want a function connected to an event, so it is in effect called each time the event is fired, then you use the connect()
method of events.
On line two, you are changing the brick color incorrectly. BrickColor.new()
takes a string as the argument.
BrickColor.new("Really red")
To check if the brick that touched it is an actual player, we must use the hit
parameter. This parameter is built in to all Touched events and is equal to the specific Part that touched it. Although you can name this parameter whatever the heck you want, it will always equal the same thing.
If the Part that touched it belongs to a Player, it's Left Leg for example, the Parent of that Part will be the Player's character model.
From there, we can check if a Humanoid exists in hit.Parent
. If hit
is a Part that belongs to a character, it will have a Humanoid in its character model.
Unfortunately, checking for a Humanoid doesn't always work. If an NPC (non-player character) touches the brick with the Touched event, it will have a Humanoid, but it won't belong to any player. To get around this, we use the GetPlayerFromCharacter(character)
method of the Players service. If hit.Parent
equals a character model, this method will return the Player who is controlling that character. But if it doesn't equal a character model, it will return nil. We can therefore use this to make sure the person touching it is actually a Player.
Final product:
function onTouch(partThatTouchedIt) if game.Players:GetPlayerFromCharacter(partThatTouchedIt.Parent) then script.Parent.BrickColor = BrickColor.new("Really green") end end script.Parent.Touched:connect(onTouch)
script.Parent.Touched:connect(function(hit) -- hit Is a Parameter you can use inside this script only if hit.Parent:FindFirstChild("Humanoid") then -- Checks for a humanoid as only players have them script.Parent.Brickcolor = Brickcolor.new(Really green) -- Changes Brick Color end -- Ends the Function