What is Wrong with this Slow Part Script

hello I need help with this Script
When I tried it there Where no errors
and am still Kind a of a Beginner

local humanoid = game:FindFirstChild("Humanoid")

Part.Touched:Connect(function()
    if humanoid then
        print("A HUMANOID OMG")
        humanoid.WalkSpeed = 3
        print("No Speed")
    end
end)

ty for your help

It would be like this:

Part.Touched:Connect(function(Hit)
	local Character = Hit.Parent
	local IsModel = Character:IsA("Model")
	local FoundHum = Character:FindFirstChildOfClass("Humanoid")
	
	if Character and IsModel and FoundHum then
		FoundHum.WalkSpeed = 3 
	end
end)

The issue with your script is that you’re trying to find a humanoid under the game object that doesn’t exist. The touched event gives you the instance that is touched. For example, let’s say Hit is a right hand. The code checks if the right hand parent is a model(character) and then if the character is a humanoid. If all of these are true, we confirmed it’s a legitimate character so we later changed it’s humanoid walkspeed.

Okay so, you’re trying to make a part that slows down the player when touched?
First of all, you getting the humanoid at the start is completely wrong for 3 reasons:

  1. The humanoid has likely still not loaded, meaning the humanoid variable will be equal to nil (no value).
  2. Even if the whole game loaded, this would only affect a single humanoid.
  3. This will run whenever ANYTHING touches the part, meaning even if you correctly have the humanoid it would affect them before even touching the part.

Right now, you’re checking “if humanoid then”, which will not run since “humanoid” is equal to nil right now.

What you should do is this instead:

Part.Touched:Connect(function(hit) --hit variable is the part that touched.
     local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid") -- this will attempt to get the humanoid.
     if humanoid then
          print("Humanoid touched!")
          humanoid.WalkSpeed = 3
     end
end)

This should work as you intended. If you have any issues tell me cause I wrote the code in a hurry