Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

i make this basic script that change the part and it didnt worked why?

i make this basic script

block = script.Parent
on = true


block.Touched:connect(function()
	if on == true then
		block.BrickColor = BrickColor.new('Really red')
		on = false
		wait(1)
	else
		block.BrickColor = BrickColor.new('Lime green')
	end
end)

but as soon i started the game the part is already changed to red i dont even touched the part but when i touched it, just change to green and cant change back to red i'm still learning on scripting

1 answer

✓ Accepted answer

Nice work and welcome to scripting!

The reason why the part starts red is because the part has probably already touched another part that is NOT you. This could be the platform under the part or a part that's already touching it. This happens because the Touched event fires when any other part touches that part. To fix this, you will need to check if the part that touched it is one of your own character parts.

local block = script.Parent
on = true

--Inside the function() we can enter a parameter called "hit" to define the part that touches.
block.Touched:connect(function(hit)

	--We can wrap all this inside another if statement to check if "hit" is a part inside your character by finding the humanoid in the parent of "hit"
	if hit.Parent:FindFirstChild("Humanoid") then
		if on == true then
		
			block.BrickColor = BrickColor.new('Really red')
			wait(1)

			on = false
		else
			block.BrickColor = BrickColor.new('Lime green')
		end
	end
end)

2 comments

0

oh okay thanks for the explanation now i know what is going on the part is touching the Baseplate thanks :)

This discussion is preserved as it was. New answers, comments, and votes are closed.