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

ClickDetector not changing the player's CFrame when left clicked?

local btn = script.Parent.btn
local location = script.Parent.location

btn.ClickDetector.MouseClick:Connect(function(plr)
	local humanIsTrue = plr:FindFirstChild("HumanoidRootPart")
	if humanIsTrue then 
		humanIsTrue.CFrame = location.CFrame + Vector3.new(0, 2, 0)
	end
end)

In Workspace, a model named "tp" (teleport) has these Children:

A script with the above code inside A part named "btn" with a ClickDetector A part named "location"

Please let me know a fix ASAP

6 comments

0

Out of curiosity, before this code I replaced the code in lines 5 through 8 with print(plr, ", ", plr.Parent), and I got my username and Players

0

It seems like you are using Vector3 on CFrame, try CFrame.new("0, 2, 0") on the line 7.

0

I hope you are kidding, @Igoralexeymarengobr.

0

wdym im not good like u with coding tho but im pretty sure that was the problem ;p

1

@Igoralexeymarengobr no, CFrame and Vector3 are compatible

0

oh thats nice i did not know that lol

1 answer

✓ Accepted answer

The body parts are found in the Character property of the player instance.

plr:FindFirstChild("HumanoidRootPart") thus evaluates to nil, since the body parts are found in the character model, not the player instance itself.

All rig types, R6, R15, and Rthro have a HumanoidRootPart, and nothing else can fire the MouseClick event, so checking for the root part is redundant.

local btn = script.Parent.btn;
local location = script.Parent.location;

btn.ClickDetector.MouseClick:Connect(function(client)
	--// HumanoidRootPart is the PrimaryPart, so I just used the :SetPrimaryPartCFrame function of Model

	client.Character:SetPrimaryPartCFrame(location.CFrame*CFrame.new(0, 2, 0));
	--// multiplying by cframe because with vector3 you factor in collision
end);

6 comments

0

why is the ; on the end of pretty much everything?

0

Just coding habits from other languages, don't worry about it. Been using too much C++, so go blame bjarne for making an epic language

0

does it even work like that lol?

0

Thx so much @incapaxx. Btw, why was there a "*" instead of a "+" beside "location.CFrame"? I thought * meant multiply.

0

It does, this is just matrix multiplication. https://en.wikipedia.org/wiki/Matrix_multiplication

0

ok, thx for the answer and that link. ive been wanting the answer for about a week now.

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