Character Selection collision

I made a character change system for my game, and there’s this bug where if you’re standing near a wall or under something low and you switch to a bigger character, the new one doesn’t fit properly and ends up getting pushed through the ceiling or onto the roof.

Anyone know a good way to prevent this or handle the collision better? or maybe even some feedback on the script in general. i would really appreciate some help

Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.CharChange.OnServerEvent:Connect(function(player, modelName)
	local model = ReplicatedStorage.Characters:FindFirstChild(modelName)
	if not model then
		warn("Character '" .. modelName .. "' not found!")
		return
	end

	local newChar = model:Clone()
	newChar.Name = player.Name

	local humanoid = newChar:FindFirstChildWhichIsA("Humanoid")
	local hrp = newChar:FindFirstChild("HumanoidRootPart")
	if not humanoid then
		warn("Model '" .. modelName .. "' has no Humanoid!")
	end
	if not hrp then
		warn("Model '" .. modelName .. "' has no HumanoidRootPart!")
	end

	local oldChar = player.Character
	local pos = CFrame.new(0, 5, 0)

	if oldChar and oldChar:FindFirstChild("HumanoidRootPart") then
		pos = oldChar.HumanoidRootPart.CFrame
		oldChar:Destroy()
	end

	newChar.Parent = workspace
	player.Character = newChar
	newChar:MoveTo(pos.Position)

	humanoid = newChar:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local animateScript = model:FindFirstChild("Animate")
		if animateScript then
			local animateClone = animateScript:Clone()
			animateClone.Parent = newChar
			print("Animate script from model added to character.")
		else
			warn("Animate script not found in model '" .. modelName .. "'.")
		end
	else
		warn("Humanoid not found after cloning character!")
	end
end)

I think it would be best to use raycasts.

Every time you try to switch characters, it raycasts from the HumanoidRootPart (or other central part in the model) in all directions. And with that, you can determine whether there isn’t enough height or width in the room for a bigger character to fit.

Also one advice is to make pos dynamic. Raycast down and find the floor, then move the newChar up to the floor’s CFrame + half the newChar’s BoundingBox height.

Use PivotTo over MoveTo. The latter method considers obstructive geometry in the specified area. As @halosviel stated, it’s best you define your own restrictions on where players can change characters

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