Whats wrong with my script?

Hello I wrote this script and it doesnt give out any errors, it just doesnt move the hitbox.

if #players > 0 then
	
	for i = 0, 150, 1 do
		script.Parent:TranslateBy(script.Parent.HitBox.CFrame.LookVector)
		wait(0.01)
	end
end
2 Likes

Do you want it to keep going forwards via LookVector?

2 Likes

I watched smth on youtube and it worked for him, exactly the same. I want it to translate it by 150

1 Like

What do you mean by this? ‘150’ of what?

I want it to move 150 studs… (the hitbox)

1 Like

Try this out instead:

local model = script.Parent
local hitbox = model.Hitbox

if #players > 0 then
	for i = 0,150 do
		model:PivotTo(model:GetPivot() + (hitbox.CFrame.LookVector))
		task.wait()
	end
end

Hey, i have been helping him on Discord so far, turns out his issue is not with :TranslateBy(), it’s that his code doesn’t even run.

1 Like

Is #players greater than zero? Does a print run inside of that if statement? If not, what’s the rest of the script?

I realized that the whole script dont run except the first lines (this is just a little part of the script)

The Reason for this is because (Assuming its a for loop) you either not putting the Script into ServerScriptService or not Specifying what it is.

I Believe i created a fix:

function GetPlayers ()
		for _,PlayerCount in pairs({game.Players:GetPlayers()}) do -- Turns Players into a Table
		
		if #PlayerCount < 0  then
			
			print("More than 0 Players Here") -- Used for Debugging
			for _,i in pairs(game.Players:GetPlayers()) do -- Gets Player Instances
				
				i.Character:MoveTo(Location + Vector3.new(0,2,0)) -- Set this whatever
			end
		end
		
	end

end

wait(1) -- Waits one so if the Player joins, the Script will recognize it before it calls the function
local Success, Error = pcall(function() -- Protected Call to Prevent Errors (Remove if you want)
		GetPlayers()
	end)
if Success then -- Checks if the pcall was a Success or Errors
print("Completed Function")
else
warn("Failed to Complete function: "..Error)
end

The reason why it wasnt working is because the code stops working after this:

while true do
	
	for i = 15,0,-1 do
		script.Parent.Parent.Lane1Count.BillboardGui.Count.Text = "Leaving in "..i.." seconds!"
		wait(1)
	end
end

I have no clue why tho…

here’s the full script:

local TeleportService = game:GetService("TeleportService")


while true do
	
	for i = 15,0,-1 do
		script.Parent.Parent.Lane1Count.BillboardGui.Count.Text = "Leaving in "..i.." seconds!"
		wait(1)
	end
end


local players = {}

for i, v in pairs(script.Parent.Seats:GetChildren()) do
	if v.Occupant then
		print("Occupant found. "..v.Parent.Name)
		table.insert(players,game.Players:GetPlayerFromCharacter(v.Occupant.Parent))
	end
end

if #players > 0 then
	
	for i = 0, 150, 1 do
		script.Parent:TranslateBy(script.Parent.HitBox.CFrame.LookVector)
		wait(0.01)
	end
end

local serverData = TeleportService:ReserveServer(11106403765)

TeleportService:TeleportToPrivateServer(11106403765, serverData, players)

for i = 0,150,1 do
	script.Parent:TranslateBy(script.Parent.HitBox.CFrame.LookVector)
	wait(0.01)
end

The while loop is the reason for this, the thing with script is when running loops, it repeats until it is finished, but doesnt run the code below it until so. you appear to be:

while true do -- This is a while loop, it will run forever due to it being just true
	
	for i = 15,0,-1 do -- this is a for loop
		script.Parent.Parent.Lane1Count.BillboardGui.Count.Text = "Leaving in "..i.." seconds!"
		wait(1)
	end
end
print("Finished") -- This will never print because the loop will never end
1 Like

Yeah I thinked about this and was pretty sure it was this, how can I fix this?

Simply remove the while loop, its not really needed, plus for the loop, you can just do this:
(Example tho, take this with a grain of salt)

local Number = 15
for i = 1,15 do
Number -= 1
		script.Parent.Parent.Lane1Count.BillboardGui.Count.Text = "Leaving in "..Number.." seconds!"
		wait(1)
	end

Due to the way this is written, the rest of the code will run after 15 seconds

1 Like

Thank you so so much! I really appreciate it❤️

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.