Unable to find a character to use in a function (No Error)

The settings of the Collision Group are right? all parts should not collide with that same group

Where is the script currently located, and what kind of Script is it (either by class name or ScriptContext)? I’m testing your code now and it is working as you expect it, provided the Points value is set in the server, and the Script itself is running in the server (to be specific, it is parented under ServerScriptService).

I just checked the collision group editor and they are not present in the list of collision groups (during test) is this normal?

ServerScriptService, That is where i keep most of my server scripts

2 Likes

So you mean… the function SetColGroupPlr() does run.
But you dont see any change on the collision group editor tab of that group? No parts added?

Print the collision group parts in it.

I would suggest different approaches, but if @Y_VRN works for them, then, it should work, there is something wrong on your setup

Yes, all parts of my character are given the collision group, I gtg now but I will come back tomorrow

2 Likes

I still can’t reproduce the issue even with running a command (in the server). When you have time, try this test code I have as a separate Script under ServerScriptService. This is the code I used to test your original Script:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstats
	local points
	
	if (not leaderstats) then
		leaderstats = Instance.new("Folder", player)
		leaderstats.Name = "leaderstats"
	else
		points = leaderstats:FindFirstChild("Points")
	end
	
	if (not points) then
		points = Instance.new("NumberValue", leaderstats)
		points.Name = "Points"
	end

	---- test number strobing for debug
	while true do
		points.Value = 0
		task.wait(15)
		points.Value = 100
		task.wait(15)
	end
end)

The above code is for confirming your code is functional on your end.

Your issue most likely resides because of this while loop. When you have a loop mixed with other functions, it might cause issues because the code will get stuck on it (and because it is an endless loop, it will never reach the code under it). Meaning you might have your collisions created, but you will never have them set to false.

To fix this, I’d recommend checking to see when the points value changes and then set the collision group (you will need to get rid of that while loop or have it in another script).

1 Like

Try this instead

local ps = game:GetService("PhysicsService")
ps:CreateCollisionGroup("fiftyPointdoor")
ps:CreateCollisionGroup("50Pointplayer")

local fiftyPdoor = game.Workspace.fiftyPdoor

local function SetColGroupDoors(door, group)
	ps:SetPartCollisionGroup(door, group)
end

SetColGroupDoors(fiftyPdoor, "fiftyPointdoor")

local function SetColGroupPlr(char, group)
	for i, child in ipairs(char:GetChildren()) do
		if child:IsA("BasePart") then
			ps:SetPartCollisionGroup(child, group)
		end
	end
	char.DescendantAdded:Connect(function(desc)
		if desc:IsA("BasePart") then
			ps:SetPartCollisionGroup(desc, group)
		end
	end)
end

game.Players.PlayerAdded:Connect(function(plr)
    while true do
        wait(1)
	if plr.Character and plr.leaderstats.Points.Value >= 50 then
            SetColGroupPlr(plr.Character, "50Pointplayer")
        end
    end
end)

ps:CollisionGroupSetCollidable("fiftyPointdoor", "50Pointplayer", false)

i would personally remove the

plr.CharacterAdded:Connect(function(char)

and replace it with

local char = plr.Character or plr.CharacterAdded:Wait()

Thanks for helping everyone! I never expected to see this many people come and help and I am very grateful! I would never be able to make games without you guys.

2 Likes

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