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

Why does the touch event fire off automatically?

 local Touchblock = game.Workspace.Touchblock

 local function hmm1()
	if Touchblock.Touched:Connect(hmm1)
	then Touchblock.BrickColor = BrickColor.new("Really red")
	         return("success")
	    end
	end
	

 hm2 = hmm1()
print(hm2)


Whenever I press play to run over to the part, "Touchblock" to you know, touch it. It's already red and "success" has been printed. I made sure the block was anchored above ground and nothing was touching it. What's going on lol i've been stuck on this for half an hour

3 comments

0

Make your question more clearly please, we don't know what you want to achieve.

0

Well, i'm trying to touch the block, which THEN activates the event, changes the brickcolor and returns "success" in that order specifically

0

why are you only having Touched run once

1 answer

✓ Accepted answer

Your problem is the touch event will fire everytime something TOUCH it. Whatever the thing is touch it, it still fire. So if you want only a player touch it, you should put a simple if-then statement:

local Players = game:GetService("Players") --we need this service to check the thing is touching the part is player or not

local function hmm1()
	if Touchblock.Touched:Connect(hmm1) then
		if Players:GetPlayerFromCharacter(hmm1.Parent) then --this will check is the thing touching it is a player or not
		Touchblock.BrickColor = BrickColor.new("Really red")
		return("success")
		end
	end
end

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