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

I need help with variables ?

Asked by
xdeno 187
7 years ago

Hi, this is my second question and I would quickly like to say this is a great website and I really appreciate everyone's support. So I have this script and what it does is when the ball touches a part on the goal line it is counted as a goal and the player and ball is teleported to the start which is does. What I want it to do is that once 3 goals are scored it teleports the player and ball to a different place, the script below just teleports the player and ball to the start where as I want to teleport it to a different position. I'm new to scripting so sorry if this is something very basic but I would really appreciate some help, thank you.

Goals = 0
ballname = "Ball"

function onTouch(hit)
    if hit.Name == ballname then 
    wait(0.1)
        game.Workspace.Ball.CFrame=CFrame.new(-276, 46.4, 163)      
        target = CFrame.new(-262.5, 44.5, 160)
for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("Torso") then
      player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)
Goals = Goals + 1
if Goals == 3 then
Goals = true
if true then
    wait(0.1)
    game.Workspace.Ball.CFrame=CFrame.new(-300, 46.4, 163)      
        target = CFrame.new(-320, 44.5, 160)
for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("Torso") then
      player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)

end   
end
end
end
end
script.Parent.Touched:connect(onTouch)
0
Try using multiple if statements when you teleport the player. So that it checks if the player has 3 scores, if so: go here, otherwise go to start. TestingPlace1337 51 — 7y
0
I honestly don't know what you mean by that, could you write some sample code please ? xdeno 187 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
local Goals = 0
ballname = "Ball"
local target = CFrame.new(-262.5, 44.5, 160)
local goal2 == false
local target2 = CFrame.new(-320, 44.5, 160)

function onTouch(hit)
    if hit.Name == ballname then
    wait(0.1)
    hit.CFrame = CFrame.new(-276, 46.4, 163)
        for i,v in pairs(game.Players:GetChildren()) do
            if v.Character:FindFirstChild'Torso' then -- removed the "if player.Character" as it was unnecessary, you just need to check if the player.Character has a torso, and if there is no player.Character then it would not work anyways.
                v.Character.Torso.CFrame = target + CFrame.new(0, i*5, 0)--Replaced vector3.new with CFrame.new, as you cant add CFrame and Vector3. BTW i is undefined, unless you are referring to the i in the pairs loop, if you are then theres really no point as i is == How many players there is, which in this case is pointless. Unless you are trying to multiply the amount of players with 5, then keep going.
                Goals = Goals + 1
            if Goals == 3 then
                goal2 = true
            if goal2 == true then
                wait(0.1)
                hit.CFrame = CFrame.new(-300, 46.4, 163)
                if v.Character:FindFirstChild'Torso' then
                    v.Character.Torso.CFrame = target + CFrame.new(0, i*5, 0)
                end
            end
            end
            end
        end
end

script.Parent.Touched:connect(onTouch)

This should be the fixed version, please comment back if it still doesnt work. :)

0
also, you made a lot of "ipairs" loops which shouldve been in pairs loops, and you shouldve only done one. ObscureIllusion 352 — 7y
0
Oops, silly me, accidentally put two ='s on line 4, change it just to one. :) ObscureIllusion 352 — 7y
0
Thank you very much, sorry I didn't reply I forgot to come on this website. xdeno 187 — 7y
0
Sorry, I just test the script out and it doesn't seem to work and I'm unsure of what is not working. The script didn't work before when I tried to do this and I'm unsure of the problem. The script doesn't teleport the ball at all. xdeno 187 — 7y
Ad

Answer this question