Script not working when parent is changed

So, i’m re-making an old game that i made a while ago. I’m trying to make the player not be able to collide with the bot, which works, but not as expected. I’m making there be multiple maps, so the maps are stored in rep storage. If the map is in rep storage the collision script wont work, even if I make a script changing the parent to workspace. But if the robot/map is parented to the workspace(without a script) it works. If someone can please explain this to me then i’ll greatly appreciate it. here’s my script, it’s a server script.


	if script.Parent.Parent.Parent == game.Workspace then
		local PS = game:GetService("PhysicsService")
		local npc = script.Parent

		PS:CreateCollisionGroup("PlayersGroup")
		PS:CreateCollisionGroup("NpcsGroup")

		for _,v in pairs(npc:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("Part") or v:IsA("MeshPart") then
				PS:SetPartCollisionGroup(v,"NpcsGroup")
			end
		end

		game.Players.PlayerAdded:Connect(function(plr)
			plr.CharacterAdded:Connect(function(char)
				wait()
				for _,v in pairs(char:GetChildren()) do
					if v:IsA("BasePart") or v:IsA("Part") or v:IsA("MeshPart") then
						PS:SetPartCollisionGroup(v,"PlayersGroup")
					end
				end
			end)
		end)
		PS:CollisionGroupSetCollidable("PlayersGroup", "NpcsGroup", false)

	end


According to the wiki, your solution should work. Since it’s not, maybe try doing this:

Scripts have a property called Disable. Set it to true.

When you script moves the map over to workspace, have it find all Script's inside of the map and set Disabled to false.


Honestly, what you are doing isn’t the best option. I would instead have a separate script that will always create and update the CollisionGroup of players. Also, once you move a map, you would call a function that will set those parts to the NpcsGroup

nope, didn’t work sadly :frowning:
nope, didn’t work sadly :frowning:
nope, didn’t work sadly :frowning:

Yeah, you will have to go with the more proper solution.

For example, Script 1:

local PS = game:GetService("PhysicsService")
PS:CreateCollisionGroup("PlayersGroup")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		wait()
		for _,v in pairs(char:GetChildren()) do
			if v:IsA("BasePart") or v:IsA("Part") or v:IsA("MeshPart") then
				PS:SetPartCollisionGroup(v,"PlayersGroup")
			end
		end
	end)
end)

Same script with your map changer:

// maybe code above...
local PS = game:GetService("PhysicsService")
PS:CreateCollisionGroup("NpcsGroup")

function updateNpcsCollisionGroup()
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("Part") or v:IsA("MeshPart") then
			PS:SetPartCollisionGroup(v,"NpcsGroup")
		end
	end

	if PS:CollisionGroupsAreCollidable("PlayersGroup", "NpcsGroup") then
		PS:CollisionGroupSetCollidable("PlayersGroup", "NpcsGroup", false)
	end
end
// maybe code below...

// on map change...
updateNpcsCollisionGroup()

Didn’t work :(, i think the reason why it isn’t working is because it adds the player to the group when a player is added, so maybe we should try and make the player be added to the player group without the function.

Any errors visible on the console?

with your script or my original one?

lol, I just fixed it by making the robot be parented to the workspace, but when the map is added the robot will go to the map lol.