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

My teleport script doesn't work?

Asked by
lucas4114 607 Moderation Voter
9 years ago

So, I made this.. All of it works in studio testing, but when I try ingame heres what happens: The Gui's text changing works (The button saying stuff like: "Teleporting to base in: 3" that for example), the force field on the teleport to base works, anchoring the player and unanchoring the player works... The problem is the teleport part... It doesn't work, it doesn't bring the player where it's ment to go, it doesn't do anything..... Just leave the player where it is, umm but it still works in studio testing..? :/ Nothing in output.. Help me?...??.. :c

PlayerInBattle = script.Parent.PlayerInBattle

function clicked()
    local battletarget1 = CFrame.new(42.983, 14.6, -49.147)
    local battletarget2 = CFrame.new(-40.468, 14.6, -49.133)
    local battletarget3 = CFrame.new(-42.749, 14.6, 41.207)
    local battletarget4 = CFrame.new(40.703, 14.6, 41.193)
    local spawntarget = CFrame.new(189.87, 69.6, -216.3)
    local player = script.Parent.Parent.Parent
    local Torso = player.Parent.Character:WaitForChild ("Torso")
    if PlayerInBattle.Value == false then
        script.Parent.Text = "Entering battle in: 3"
        Torso.Anchored = true
        wait(1)
        script.Parent.Text = "Entering battle in: 2"
        wait(1)
        script.Parent.Text = "Entering battle in: 1"
        wait(1)
        local randombattlespawn = math.random(1,4)
        if randombattlespawn == 1 then
            local Torso1 = player.Parent.Character:WaitForChild ("Torso")
            Torso1.CFrame = battletarget1
            PlayerInBattle.Value = true
        end
        if randombattlespawn == 2 then
            local Torso1 = player.Parent.Character:WaitForChild ("Torso")
            Torso1.CFrame = battletarget2
            PlayerInBattle.Value = true
        end
        if randombattlespawn == 3 then
            local Torso1 = player.Parent.Character:WaitForChild ("Torso")
            Torso1.CFrame = battletarget3
            PlayerInBattle.Value = true
        end
        if randombattlespawn == 4 then
            local Torso1 = player.Parent.Character:WaitForChild ("Torso")
            Torso1.CFrame = battletarget4
            PlayerInBattle.Value = true
        end
        Torso.Anchored = false
    script.Parent.Text = "Click here to teleport back to base!"
    elseif PlayerInBattle.Value == true then
        script.Parent.Text = "Teleporting to base in: 3"
        Torso.Anchored = true
        wait(1)
        script.Parent.Text = "Teleporting to base in: 2"
        local ff = Instance.new ("ForceField")
        ff.Parent = Torso.Parent
        wait(1)
        script.Parent.Text = "Teleporting to base in: 1"
        wait(1)
        PlayerInBattle.Value = false
        local Torso1 = player.Parent.Character:WaitForChild ("Torso")
        Torso1.CFrame = spawntarget
        Torso.Anchored = false
        script.Parent.Text = "Click here to teleport into battle!"
        ff:Remove()
    end
end

script.Parent.MouseButton1Click:connect(clicked)

2 answers

Log in to vote
1
Answered by
jman116 10
9 years ago
 if randombattlespawn == 1 then
             player.Parent.Character:MoveTo(battletarget1)
            PlayerInBattle.Value = true
   end

Change the battletarget1,2,3,4's Cframe to a Vector3 value, then change Torso1.Cframe = Cframe.new to player.Parent.Character:MoveTo(battletarget1,2,3,4)

0
Hmm, I'll try this then.. lucas4114 607 — 9y
0
works :D lucas4114 607 — 9y
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

I don't really know why your code is working.. but all your positions to teleport to are very close to eachother.. so maybe you are moving but you don't notice?

Anyways.. I'm going to suggest an alternative for selecting the teleport. If you make a table for all the locations, then index the table randomely.. it will effectively select a random location.

What i mean by indexing a table randomely;

local a = {3,2,1}
--I have a table 'a' with 3 indexes. I want to get the first index
print(a[1]) --> '3'
--Now, if we put the math.random function inside of the brackets then it will index it randomely
print(a[math.random(1,2)])
--But there are 3 indexes in the table.. so we should use '3' as the max number.
print(a[math.random(1,3)])
--[[And to just make things easier.. in case you wanna add more indexes without having to change that number then just use the '#' operator on the table. It will return how many indexes are in the table]]
print(a[math.random(1,#a)])

Now, to use this to your purposes;

PlayerInBattle = script.Parent.PlayerInBattle

function clicked()
    --Table of locations
    local battleTargets = {
        CFrame.new(42.983, 14.6, -49.147),
        CFrame.new(-40.468, 14.6, -49.133),
        CFrame.new(-42.749, 14.6, 41.207),
        CFrame.new(40.703, 14.6, 41.193)
    }
    local spawntarget = CFrame.new(189.87, 69.6, -216.3)
    local player = script.Parent.Parent.Parent
    local Torso = player.Parent.Character:WaitForChild ("Torso")
    if not PlayerInBattle.Value then
        Torso.Anchored = true
        --Count down with a for loop, eh?
        for i = 3,1,-1 do
            script.Parent.Text = "Entering battle in: "..i
            wait(1)
        end
        --Index table randomely
        local Spawn = battleTargets[math.random(1,#battleTargets)]
        --'Torso' is already defined.. no need to redefine it
        Torso.CFrame = Spawn
        Torso.Anchored = false
        script.Parent.Text = "Click here to teleport back to base!"
    --No need for elseif, just use 'else'
    else
        Torso.Anchored = true
        local ff = Instance.new('ForceField',Torso.Parent)
        --Yay for for loops c:
        for i = 3,1,-1 do
            script.Parent.Text = "Teleporting to base in: "..i
             wait(1)
        end
        PlayerInBattle.Value = false
        Torso.CFrame = spawntarget
        Torso.Anchored = false
        script.Parent.Text = "Click here to teleport into battle!"
        wait(2)
        ff:Destroy()
    end
end

script.Parent.MouseButton1Click:connect(clicked)
0
Hmh... Uhm.. This doesn't help...... :/ lucas4114 607 — 9y
0
It shows a more efficient method for how to do what you're wanting to do. But there's nothing wrong with your code Goulstem 8144 — 9y

Answer this question