Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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!

0
It just says modelname="teleporter1b".. Make it to this.. local modelname = "teleporter1b" crywink 419 — 7y
0
put local before model name include 1 space abnotaddable 920 — 7y
0
Okay, thank you, I did this. I'm not sure what else is wrong with it. callmehbob 54 — 7y

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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.

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 callmehbob 54 — 7y
0
errors in the output :( thank you very much for trying to help me! callmehbob 54 — 7y
0
My mistake; I realise what I did. One moment. Pyrondon 2089 — 7y
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. Pyrondon 2089 — 7y
View all comments (3 more)
0
Understood! And it works! Thank you so much, you are the best! callmehbob 54 — 7y
0
Thank you for the explanation and link as well!! callmehbob 54 — 7y
0
No problem! Pyrondon 2089 — 7y
Ad

Answer this question