Part feeling like colliding when its not supposed to

Hello! Third post on here. Still a beginner dev but I’m trying. I have a set of scripts to set ownership of a tycoon(in dev name) to a player, and it’s working. I have another script that turns off collision for the “Gate” if the player owns the tycoon. Here’s the code:

--Server Code
local RS = game:GetService("ReplicatedStorage")
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("PlayerData")
local TC = game.Workspace.Tycoons:GetChildren()


RS.InitBase.OnServerEvent:Connect(function(player)
	print("test working! player is "..player.Name)
	
	for i,v in TC do
		
		if v.Gate:GetAttribute("HasOwner") == true then
			return
		end
		
		if v.Gate:GetAttribute("HasOwner") == false then
			print("attempted to set "..v.Name.." to "..player.Name)
			v.Gate:SetAttribute("Owner", player.Name)
			v.Gate:SetAttribute("HasOwner", true)
			v.Gate.Color = Color3.fromRGB(170, 0, 0)
			
		end
		
		if v.Gate:GetAttribute("Owner") == player.Name then
			warn("dont")
		end
	end
	
end)
--Client code
while true do
	if game.Players.LocalPlayer.Name ~= script.Parent:GetAttribute("Owner") then
		script.Parent.CanCollide = true
	end
	
	if game.Players.LocalPlayer.Name == script.Parent:GetAttribute("Owner") then
		script.Parent.CanCollide = false
	end

end

P.S Sorry for not replying in my previous topic - I forgot

2 Likes

This is not how you’re supposed to handle collisions, because this is very inefficient. You should be using CollisionGroups, adding 1 for each tycoon gate. Then, set it up so that only the owner doesn’t collide (by adding another collision group for just the owner, and setting collisions to false).

Code (make sure to remove your client script):

--Server Code
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")

--//Variables
local DS = DSS:GetDataStore("PlayerData")
local Tycoons = workspace.Tycoons:GetChildren()

--//Functions
RS.InitBase.OnServerEvent:Connect(function(player)
	print("test working! player is "..player.Name)

	for i, v in Tycoons do
		if v.Gate:GetAttribute("HasOwner") then
			return
		end

		print("attempted to set "..v.Name.." to "..player.Name)
		
		if not PhysicsService:IsCollisionGroupRegistered(i) then			
			PhysicsService:RegisterCollisionGroup(i)
		end
		
		if not PhysicsService:IsCollisionGroupRegistered(player.UserId) then			
			PhysicsService:RegisterCollisionGroup(player.UserId)
			PhysicsService:CollisionGroupSetCollidable(i, player.UserId, false)
		end
				
		if player.Character then
			for i, descendant in ipairs(player.Character:GetDescendants()) do
				if descendant:IsA("BasePart") then
					descendant.CollisionGroup = player.UserId
				end
			end
		end
		
		v.Gate:SetAttribute("HasOwner", true)
		v.Gate:SetAttribute("Owner", player.UserId)
		v.Gate.CollisionGroup = i
		v.Gate.CanCollide = true
		v.Gate.Color = Color3.fromRGB(170, 0, 0)
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		if not PhysicsService:IsCollisionGroupRegistered(player.UserId) then
			return
		end
		
		for i, descendant in ipairs(character:GetDescendants()) do
			if descendant:IsA("BasePart") then
				descendant.CollisionGroup = player.UserId
			end
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	if PhysicsService:IsCollisionGroupRegistered(player.UserId) then
		PhysicsService:UnregisterCollisionGroup(player.UserId)
	end
end)
4 Likes

I’ve also noticed you’re using some unnecessary attributes, like for “HasOwner”, you can just check if “Owner” is valid, no need for another attribute.

3 Likes

That’s the solution. I saw the documentation for CollisionGroups, i just didn’t know if this was an appropriate use. Thanks!

4 Likes

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