Scripting help. (Making a find the button game)

I need a script that works for when clicking a button you will teleport - I have found a script but it doesn’t work.

This was the script:

"function onClicked(Player)
local torso = Player.Character:findFirstChild(“HumanoidRootPart”)
if torso ~= nil then
local playertest = Player.Character:findFirstChild(“Humanoid”)
if playertest ~= nil then
if playertest.Health > 0 then
–Set the position of the brick you want to teleport to into the “location” variable
local location = game.Workspace.floor1.Position
local x = location[“20.22”]
local y = location[“2.075”]
local z = location[“63.918”]

			local cf = torso.CFrame
			torso.CFrame = CFrame.new(Vector3.new(x,y,z))
		end
	end
end

end

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

I did edit it, but I am not sure what to do.

Format code moment

function onClicked(Player)
    local torso = Player.Character:findFirstChild(“HumanoidRootPart”)
    if torso ~= nil then
        local playertest = Player.Character:findFirstChild(“Humanoid”)
        if playertest ~= nil then
            if playertest.Health > 0 then
                –Set the position of the brick you want to teleport to into the “location” variable
                local location = game.Workspace.floor1.Position
                local x = location["20.22"]
                local y = location["2.075"]
                local z = location["63.918"]

			    local cf = torso.CFrame
			    torso.CFrame = CFrame.new(Vector3.new(x,y,z))
		    end
	    end
    end
end

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

It looks as though your defining the location’s X/Y/Z variables by just regular numbers, which won’t work since you didn’t even define them

local x = location["20.22"]
local y = location["2.075"]
local z = location["63.918"]

I think to fix this, you can do:

local x = location.X
local y = location.Y
local z = location.Z

Another thing I noticed is this:

torso.CFrame = CFrame.new(Vector3.new(x,y,z))

I don’t believe you need to add a new Vector3 in the mix as well, you just need CFrame.new?

torso.CFrame = CFrame.new(x, y, z)
3 Likes

It works! Thank you so much! Very very useful.

1 Like

So as I say: First of all, make a part that you would like to be teleported to after the button is clicked.(name it Teleport) and then add a click detector to your button(name it button) and then paste the following script:

local btn = game.Workspace.button
local player = game.Players.LocalPlayer
local char = player.Character

if btn.ClickDetector.Activated = true then
char.Humanoid.HumanoidRootPart.CFrame = game.Workspace.Teleport.CFrame
end)

This may not work and if it doesn’t send the output please.