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

What do I need to change to make this teleportation script work with onClicked instead onTouched?

This script works great when you are using onTouched, but unfortunately I can't figure out how to make this work with onClicked. Could you please explain to me why this does not work with onClicked? I haven't been getting any errors so I'm not sure what to tackle next.

My script:

------------------------------------
local modelname="teleporter1b"
------------------------------------

function onClicked(part)
	if part.Parent ~= nil then
	local h = part.Parent:findFirstChild("Humanoid")
		if h~=nil then
			local teleportfrom=script.Parent.Enabled.Value
			if teleportfrom~=0 then
				if h==humanoid then
				return
				end
				local teleportto=script.Parent.Parent:findFirstChild(modelname)
				if teleportto~=nil then
					local torso = h.Parent.Torso
					local location = {teleportto.Position}
					local i = 1

					local x = location[i].x
					local y = location[i].y
					local z = location[i].z
				
					x = x + math.random(-1, 1)
					z = z + math.random(-1, 1)
					y = y + math.random(2, 3)

					local cf = torso.CFrame
					local lx = 0
					local ly = y
					local lz = 0
					
					script.Parent.Enabled.Value=0
					teleportto.Enabled.Value=0
					torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz))
					wait(3)
					script.Parent.Enabled.Value=1
					teleportto.Enabled.Value=1
				else
					print("Could not find teleporter!")
				end
			end
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Thank you!

3 comments

0

It just says modelname="teleporter1b".. Make it to this.. local modelname = "teleporter1b"

0

put local before model name include 1 space

0

Okay, thank you, I did this. I'm not sure what else is wrong with it.

1 answer

✓ Accepted answer

MouseClick event listeners have a sole parameter, playerWhoClicked. Touched event listeners' parameter, however, is the part which was touched.

The reason it isn't working is because currently part in your script would be the Player who clicked the part. You can fix this problem pretty easily:

EDIT: Somehow, I mistakenly thought that part would be the part touched, instead of the part which touched it. Instead, you should get the player's character:

------------------------------------
local modelname="teleporter1b"
------------------------------------

function onClicked(player)
	local char = player.Character; --// You can get the character just as easily, with some small changes to the function as seen below.
	local h = char:FindFirstChild("Humanoid")
	if h~=nil then
		local teleportfrom=script.Parent.Enabled.Value
		if teleportfrom~=0 then
			local teleportto=script.Parent.Parent:FindFirstChild(modelname)
			if teleportto~=nil then
				local torso = char.Torso
				local location = {teleportto.Position}
				local i = 1

				local x = location[i].x
				local y = location[i].y
				local z = location[i].z

				x = x + math.random(-1, 1)
				z = z + math.random(-1, 1)
				y = y + math.random(2, 3)

				local cf = torso.CFrame
				local lx = 0
				local ly = y
				local lz = 0

				script.Parent.Enabled.Value=0
				teleportto.Enabled.Value=0
				torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz))
				wait(3)
				script.Parent.Enabled.Value=1
				teleportto.Enabled.Value=1
			else
				print("Could not find teleporter!")
			end
		end
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

Also, though yours wasn't too bad, I still urge you to try to always practice correct indentation of blocks.
Hope this helped.

7 comments

0

Thank you very much for the explanation! "The reason it isn't working is because currently part in your script would be the Player who clicked the part. You can fix this problem pretty easily:" What exactly did you change it to, if it isn't using the player who clicked the part anymore? Or maybe I am misunderstanding? Also, I am not quite sure why, but it is still not working, and without any erro

0

errors in the output :( thank you very much for trying to help me!

0

My mistake; I realise what I did. One moment.

0

To clarify, when I said it "would be the player who clicked the part", I meant the literal Player object, instead of player.Character.

0

Understood! And it works! Thank you so much, you are the best!

0

Thank you for the explanation and link as well!!

0

No problem!

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