Strange Issue with Touched

Long Story Short, I’m currently new into scripting and I’ve been learning it for about a week now. So this line of code used to work perfectly for me a few days ago:

game.Workspace.Part.Touched:Connect(function()

game.Workspace.Part.Transparency = 0.5

end)

so obviously I’m trying to tell the script that when I touch the part it’ll turn into Transparency 0.5 but here is the Problem: When I enter this code into a script and press “run” or “play” the Part is already at Transparency 0.5 before I’ve even touched this. I’m pretty sure theres nothing wrong with this line of code unless It’s just a simple mistake I can’t spot. Would Appreciate if anyone could point out the problem, Thank you!

(edited) - PS, Same problem happens with this line of code

game.Workspace.Part.Touched:Connect(function()

game.Workspace.Brick.Transparency = 0
game.Workspace.Brick.CanCollide = true

end)

2 Likes

I can assume that the part is touching another part nearby - Touched triggers on all part collisions, not just for characters. You should have the script check what touched it before executing the change code.

game.Workspace.Part.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") ~= nil then -- This will trigger for players and NPCs
    game.Workspace.Part.Transparency = 0.5
    end
end)
5 Likes

The part that you are specifying could be by chance un-anchored.
The touched event can fire just by the part simply touching the ground.

You’ll have to perform a check to see if the part is specifically touching a player like this:

game.Workspace.Part.Touched:Connect(function(PartTouched)
 if PartTouched and game.Players:GetPlayerFromCharacter(PartTouched.Parent) then
      game.Workspace.Part.Transparency = 0.5
end
end)
5 Likes

Thank you this cleared up everything, I anchored it above the Terrain and it worked correctly this time! I’ll try it with this code and see what I can learn from it!

2 Likes

Thanks man, Really helpful. Managed to solve the problem. I’ll use your code aswell and try to see what I can learn from it!

2 Likes

Also just a question, there is a part in that script I don’t completely understand since I’m new to scripting. Why did you have to put PartTouched inside (), I never got the hang of what the () even does in a function.

2 Likes

“PartTouched” is the argument returned from the touched function.

PartTouched is the object that touched the part that have the touched even connected to.

It’s essential if you want to perform certain checks to see if the part touched a certain object.

1 Like

I believe the actual cause of the problem is that anything that touches the part will fire the Touched event, not just when a player touches a part.

Anchoring the part may remedy the issue, as if it’s anchored it’s already considered to be touching the parts, so the Touched event would not fire.

However, I have experienced situations where this does not happen, and the Touched event fires for all of the touching parts, regardless of whether they’re anchored or not.

Here’s how you would check if a player has touched it:

Part.Touched:Connect(TouchingPart)
    local Player = game.Players:GetPlayerFromCharacter(TouchingPart.Parent)
    if Player ~= nil then
        -- code
   end
end)
1 Like

Thanks man, just question:
I don’t exactly understand the symboles and “Nil” in this line if Player ~= nil then
due to me just starting out coding. Would appreciate if you could explain what those stand for so I could use them in the future when I code. I can’t find it on the Roblox Wiki.

== is ‘equal to’ when comparing two values, ~= is ‘not equal to’.

1 == 1 and 1 ~= 2.

nil is a value for ‘nothing’.

GetPlayerFromCharacter returns nothing if a player wasn’t found.

You can just write if Player then since Player is guaranteed to be either a Player object or nil.

2 Likes

Was doing it in its full form to be specific. if Player then is rather unspecific and applies to lots of different values :wink: