I'm having some issues with code logic

So basically I have got a couple parts and when they are touched they light up blue. I have tried to set it up so that, if a player touched “part1” then he cant touch “part2” but its not working for some reason. Any ideas why?

T1P1.Touched:Connect(function(player)
	if T1Player1 == nil then
		T1Player1 = player
		T1B1.BrickColor = BrickColor.new("Teal")
		T1P1.Transparency = 0.75
		T1P2.Base.Transparency = 0
		T1P2.CanTouch = true
	end
end)

T1P1.TouchEnded:Connect(function(player)
	if T1Player1 == player then
		T1Player1 = nil
		T1B1.BrickColor = BrickColor.new("Smoky grey")
		T1P1.Transparency = 1
		T1P2.Base.Transparency = 1
		T1P2.CanTouch = false
		T1P2.Transparency = 1
		T1P3.Base.Transparency = 1
		T1P3.CanTouch = false
		T1P3.Transparency = 1
	end
end)

T1P2.Touched:Connect(function(player)
	if T1Player1.Name ~= player.Name then
		if T1Player2 == nil then
			T1Player2 = player
			T1B2.BrickColor = BrickColor.new("Teal")
			T1P2.Transparency = 0.75
			T1P3.Base.Transparency = 0
			T1P3.CanTouch = true
		end
	end
end)

image

Well… Did you try debugging/printing values first before posting a forum post?
Go check what happens. First learn to debug your code first, and then if you don’t know, make a forum post.

Add print to T1P1.Touched before first condition and then into condition.
Add print to T1P1.TouchEnded before condition and condition
Same for T1P2.Touched and watch if conditions are true.

You can make a variable to check if touching.

AlreadyTouching = false --you can set this to true when the part is touched.when another part is touched.you should check the value if its true or not.if its true,it should return.if its false.it should make the variable to true.when touchends,you can set the variable false back.
1 Like

for real.i didnt understand too.i think he didnt placed the variables or the full version of the code.

Try this:

T1P2.TouchEnded:Connect(function(player)
    if T1Player2 == player then
        T1Player2 = nil
        T1B2.BrickColor = BrickColor.new("Smoky grey")
        T1P2.Transparency = 1
        T1P3.Base.Transparency = 1
        T1P3.CanTouch = false
        T1P3.Transparency = 1
    end
end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.