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

I need help with variables ?

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)

2 comments

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.

0

I honestly don't know what you mean by that, could you write some sample code please ?

1 answer

✓ Accepted answer
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. :)

4 comments

0

also, you made a lot of "ipairs" loops which shouldve been in pairs loops, and you shouldve only done one.

0

Oops, silly me, accidentally put two ='s on line 4, change it just to one. :)

0

Thank you very much, sorry I didn't reply I forgot to come on this website.

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.

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