ReplicatedStorage event is picky and only does what its supposed to when there's 1 player

this piece of code works when there’s 1 player, not when there’s 2 or more. only part that works is the collision group one(only one that wasnt written by me, coincidence???)

--in SSS
RS.InitBase.OnServerEvent:Connect(function(player)
	print("player "..player.Name.." got their base loaded via clientfire")

	for i, v in Tycoons do
		if v.Gate:GetAttribute("HasOwner") == true then
			if v.Gate:GetAttribute("Owner") == player.UserId then
				break
			end
			break
		end
		
		
		if v.Gate:GetAttribute("HasOwner") == false then
			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)
			
			RS.BaseLoaded:Fire(player, v)
			break
		end				
	end
	
1 Like

Which part exactly doesn’t work? Please share the client source and your explorer

1 Like

Its super specific, it only works properly when 1 person in total fires it, client works, BaseLoaded() works but only for the first person

1 Like

Please share the Local Script from which you fire the event

local RS = game:GetService("ReplicatedStorage")
local button = script.Parent

button.MouseButton1Click:Connect(function()
	RS.InitBase:FireServer()
end)

sorrry for delay man

Tried to rewrite it now it doesnt work at all im stumped

RS.InitBase.OnServerEvent:Connect(function(player)
	print("player "..player.Name.." got their base loaded via clientfire")

	for i, v in ipairs(Tycoons) do
		local owner = v.Gate:GetAttribute("Owner")
		local gate = v.Gate
		
		if owner ~= nil then
			print("gate of "..v.Name" is owned, finding next")
			return("fake")
		end
		
		if owner == player.UserId then
			print("player already has a base")
			return("fake")
		end
		
		if not owner then										
			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 index, descendant in ipairs(player.Character:GetDescendants()) do
					if descendant:IsA("BasePart") then
						descendant.CollisionGroup = player.UserId
					end
				end
			end
			
			v.Gate:SetAttribute("Owner", player.UserId)
			v.Gate.CollisionGroup = i
			v.Gate.CanCollide = true
			v.Gate.Color = Color3.fromRGB(170, 0, 0)

			RS.BaseLoaded:Fire(player, v)
			
		end	
	end
end)

Are there any prints with this code?

EDIT: I modified your code a little bit and added some debug prints. Could you try this?

--in SSS
RS.InitBase.OnServerEvent:Connect(function(player)
    print("Player " .. player.Name .. " got their base loaded via client fire")

    local foundTycoon = false
    for i, v in ipairs(Tycoons) do
        if v.Gate:GetAttribute("HasOwner") == true and v.Gate:GetAttribute("Owner") == player.UserId then
            -- Player already owns this base, no need to continue, you can do whatever you want here.
            foundTycoon = true
			print("[0] Found tycoon.")
            break
        end

        if v.Gate:GetAttribute("HasOwner") == false then
            print("[0] HasOwner false, continue.")
            if not PhysicsService:IsCollisionGroupRegistered(player.UserId) then
                PhysicsService:RegisterCollisionGroup(player.UserId)
                print("[0] Registered collision group for player: " .. player.UserId)
            end

            if not PhysicsService:IsCollisionGroupRegistered(i) then
                PhysicsService:RegisterCollisionGroup(i)
                print("[0] Registered collision group for base: " .. i)
            end

            if player.Character then
                for _, descendant in ipairs(player.Character:GetDescendants()) do
                    if descendant:IsA("BasePart") then
                        descendant.CollisionGroup = player.UserId
                        print("[0] Set collision group for character part: " .. descendant.Name)
                    end
                end
            end

            v.Gate:SetAttribute("HasOwner", true)
            v.Gate:SetAttribute("Owner", player.UserId)
            v.Gate.CollisionGroup = player.UserId
            v.Gate.CanCollide = true
            v.Gate.Color = Color3.fromRGB(170, 0, 0)

			print("[0] SetAttributes correctly done")

            RS.BaseLoaded:Fire(player, v)
            foundTycoon = true
            print("[0] Base assigned to player: " .. player.UserId)
            break
        end
    end

    if not foundTycoon then
        print("[1] Couldn't find a suitable tycoon for player: " .. player.UserId)
    end
end)

2 Likes

Only first print statement actually fires
Ill test that too

FINALLY some feedback from the script

Edited some values, it works with 1 player now

I saw you solved the topic? Is it really solved or do you need more help?

Nope, did some further testing, collisions dont work propely

Nevermind, i fixed it, in your code you set v.CollisionGroup = player.UserId when it should be v.CollisionGroup = i

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