PhysicsService trouble and strange behavior

  1. What do you want to achieve? I want to understand why this is happening for further coding. And to avoid such problems in the future

  2. What is the issue? I have a code that prohibits people without a staff from passing through part, when equipped, the collision is turned off, and when removed, the collision is turned on for this player. The problem is that it doesn’t work properly, sometimes part can skip the player who put on the staff and then immediately took it off.

Untitled-1
Untitl2ed-1
It work strange
Untitled-3
It normal work

The script is written normally
CollisionGroupsAreCollisible shows a positive result when removing the staff and putting on the staff, but for some reason the player himself does not want to work normally.

  1. What solutions have you tried so far? But I found a solution, but it is very strange, if you register the Touched Event on Part, then the errors disappear and everything starts to work as it should and everything works well, even if there is nothing in the Event.

Script 1 part_script

local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local electro_obj_child = workspace.Part_object:GetChildren()


local doors = "Doors"
local player_want_door = "Players_want_door"


PhysicsService:CreateCollisionGroup(player_want_door)

PhysicsService:CreateCollisionGroup(doors)

for _,  fence in pairs(electro_obj_child) do
	
	if fence:IsA("Part") then
		
		PhysicsService:SetPartCollisionGroup(fence,doors)
		
	   fence.Touched:Connect(function()
		
		end)
		
	end
	
	
	
end

Script 2 tool_script


local PhysicsService = game:GetService("PhysicsService")

local colide_off_on = true

local function add_colisian_for_player(character_player_array,player_character_just)
	
	if colide_off_on == true then
		if  not PhysicsService:CollisionGroupContainsPart(player_want_door,player_character_just.Head)   then
			

		for _,name_part_player in pairs(character_player_array) do
		
		if name_part_player:IsA("Part") or name_part_player:IsA("MeshPart") then
			
			PhysicsService:SetPartCollisionGroup(name_part_player,player_want_door)
				
		end
		
		end
		
		end  
		colide_off_on= false
		
	end
	
	PhysicsService:CollisionGroupSetCollidable(player_want_door,"Doors",false)
	

end

local function onEquip()
add_colisian_for_player(player_character:GetChildren(),player_character)
	print(PhysicsService:CollisionGroupsAreCollidable (player_want_door,"Doors"))	
end

1 Like