Help With Part Script

I am new to scripting and I’m trying to make a script that when a player touches a part the part changes color and it spawns one new part, but there is two problems, the part changes color when anything touches it (Baseplate, and other parts), and I just want it to be when a player touches it. The other problem is the script that I’m using is spawning multiple parts and I only wanna spawn one. Any feedback is helpful.

game.workspace.Part.Touched:Connect(function()
	game.workspace.Part.Color = Color3.new(0, 0, 0)
	Instance.new("Part",game.Workspace)
end)

That’s because there is no debounce so it is spawning a part everytime the touch event occurs which is… a lot.

If you want it to spawn just one time then make an if condition asking if the part has been spawned already.

local spawned = false
workspace.Part.Touched:Connect(function()
    if not spawned then
	    workspace.Part.Color = Color3.new(0, 0, 0)
	    Instance.new("Part",game.Workspace)
        spawned = true
    end
end)

The reason it is spawning multiple parts is because when you call touched function it will fire if any of ur body prts touch it. To check if the player is touching it, get the parent of the collidedpart and check if it contains a humanoid.

P.S if u need the code u can msg me

Hey! I’d recommend checking out the Learn Studio section on the developer hub. It teaches you about using touched events, as well as a bunch of other important stuff that you will be using frequently.

local spawned = false

game.Workspace.Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- Indicates that a player has touched the part
		if not spawned then -- If the part has not been spawned
			spawned = true
			game.Workspace.Part.Color = Color3.new(0, 0, 0)
			Instance.new("Part", game.Workspace)
		end
	end
end)