Detect collision and then disable it using repeat until

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a local script in startercharacter that enables collision to whatever part named “Voxel” the player touches with their left leg. And when touch ends, turn off collison of the hit.
  2. What is the issue? Include screenshots / videos if possible!
    After checking with a while, the script does not turn off collision of the hit
    My code:
local St
local hi = nil
game.Players.LocalPlayer.Character["Left Leg"].Touched:Connect(function(hit)
	if hit.Name == "Voxel" then
		St = false
		hi = hit
		repeat
			hit.CanCollide = true
			hit.Massless = false
			wait(1)
			until St == true
		hit.CanCollide = false
		hit.Massless = true
	end
end)

hi.TouchEnded:Connect(function()
	St = true
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, I searched but found no info for my problem

you are repeating the same thing without turning St to TRUE, you are only checking if ST is true

hit.CanCollide = true
			hit.Massless = false
			wait(1)
                       --where is St = true?
            until St == true

Yes, but I need it to be constant or else the player will start moving down and up all the time.
Because the player is constantly standing in 1x1x1 blocks

lets be clear

your making a part called voxel, that when is touched by the left leg, the voxel enables the collision.
and when the left leg isnt touching the voxel, it disables the collision.

is this what you mean? if i missed something tell it

Yes, thats the point.
Also, the script is located in StarterCharacterScripts.

--i removed the variables, motivation is in the comments under this one
game.Players.LocalPlayer.Character["Left Leg"].Touched:Connect(function(hit)
	if hit.Name == "Voxel" then
		hit.CanCollide = true
		hit.Massless = false
--i removed the repeat loop, because if the left leg touches it, the script does autmatically the loop

	end
end)
--you cant make hit.TouchEnded function if this isnt his script
game.Players.LocalPlayer.Character["Left Leg"].TouchEnded:Connect(function(hit)
	if hit.Name == "Voxel" then
		hit.CanCollide = false --same script but in reverse
		hit.Massless = true

	end
end)

tested

Worked. Thanks!
30charlimitfiller

1 Like