Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

CFraming a model without colliding into other models?

So I'm trying to make a model CFrame and when it touches the brick that has this script, the value I made up and is named Anchor turns to true and the model goes to Vector3.new(-20,0,0), everything is working fine, the problem is that this script breaks if a player and the model are touching this brick at the same time.

Could anyone think of a solution to fix it from breaking, or maybe another way I could do what I'm trying to do yet more efficiently?

while wait(1) do
local Stop = script.Parent:GetTouchingParts()
for i = 1,#Stop do
	
local model = Stop[i].Parent
local part = model:WaitForChild("PrimaryPart")
model.PrimaryPart = part
model:SetPrimaryPartCFrame(part.CFrame+Vector3.new(-20,0,0))

if Stop[i].Parent:FindFirstChild("Settings") then
Stop[i].Parent.Settings.Anchor.Value = true	

elseif not Stop[i].Parent:FindFirstChild("Settings") then
end	
	
end
end

1 answer

✓ Accepted answer

Your problem is that you are using WaitForChild on line 6. If a player touches the brick, that function will never return, meaning your script freezes. Instead, use FindFirstChild combined with an if statement to check if part actually exists:

while wait(1) do
	local Stop = script.Parent:GetTouchingParts()
	for i = 1,#Stop do
		local model = Stop[i].Parent
		local part = model:FindFirstChild("PrimaryPart")
		if part then
			model.PrimaryPart = part
			model:SetPrimaryPartCFrame(part.CFrame+Vector3.new(-20,0,0))

			if Stop[i].Parent:FindFirstChild("Settings") then
				Stop[i].Parent.Settings.Anchor.Value = true 		
			elseif not Stop[i].Parent:FindFirstChild("Settings") then
			end
		end
	end
end

1 comment

0

Thanks so far it seems like it's working great

This discussion is preserved as it was. New answers, comments, and votes are closed.