Script won't continue

Hi! I am trying to script a CaD that would check a plate number for a car. For some reason, when the script tries to get the name, it never continues. I’ve tried checking console for errors and such but it doesn’t give any errors.

Problematic Code:

			VehicleDB.Search.MouseButton1Click:Connect(function()
print("search")
local plateID = VehicleDB.plateID.Text
print("Got plate teext")
for i,PName in pairs(game.Players:GetChildren()) do
	--The problem starts here
	for i,WorkspaceItem in pairs(game.Workspace:GetChildren()) do
		print("for i,v") -- this gets spammed, obviously.
		if WorkspaceItem.Name == PName.Name.."'s Car" then
			print("Got user car") -- This never goes through
                            --The problem ends here

--The car code that makes the name change.
	newCar.Parent = workspace
newCar:MakeJoints()
local id = create_id(5)
newCar.Name = player.Name.."'s Car"
print(newCar.Name) -- it does get changed to the correct name..?
newCar.PlateNumbers.Value = id
newCar.Body.Plate.PlateGUI.Frame.PlateID.Text = id
return newCar
Code Including The Problem { slightly long }
VehicleDB.Search.MouseButton1Click:Connect(function()
print("search")
local plateID = VehicleDB.plateID.Text
print("Got plate teext")
for i,PName in pairs(game.Players:GetChildren()) do
	
		for i,WorkspaceItem in pairs(game.Workspace:GetChildren()) do
		print("got")
		if WorkspaceItem.Name == PName.Name .. "'s Car" then
			print("Got user car")
				print("Set")
				local Color = WorkspaceItem.Body.Model.Color.Color.BrickColor
				local Model = WorkspaceItem.Model.Value
				local Owner = PName.Name
				
				VehicleDB.Color.Text = Color.Name
				VehicleDB.Model.Text = Model
				VehicleDB.Owner.Text = Owner
				print("Set2")
			
		end
		
		end
		
	end

end)

Car Code { long and confusing.. to me }
local function SpawnCar(template, player) -- Parents cloned `template` to Workspace and creates joints
local newCar = template:Clone()
local letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

function assing_value(number)
if number <= 26 then
	number = letters[number]
	else
	number = number - 27
end
return number  
end

local function create_id(length)
local id = ""
if length > 0 then
	for i = 1, length do
		local number = math.random(1, 36)
		id = id .. assing_value(number)
	end
end
return id
end
newCar.Parent = workspace
newCar:MakeJoints()
local id = create_id(5)
newCar.Name = player.Name.."'s Car"
print(newCar.Name)
newCar.PlateNumbers.Value = id
newCar.Body.Plate.PlateGUI.Frame.PlateID.Text = id
return newCar
end

Not enough information. It would help if you tried debugging your own code or checking your console. Checking your console and attempting to resolve the problem yourself (either by debugging or reworking) should be your absolute two starting points when you encounter an issue. Next should be searching and finally should be asking here.

That being said, while you check your console, a few issues I’d like to raise:

  • GetService is the canonical way to fetch a service. Services can be renamed and it’s typically conventionally correct to use GetService, as it fetches by ClassName and not by Name.

  • GetPlayers is the canonical way to get all the players in the Players service. GetPlayers only returns Player objects. GetChildren is inherited by the Players service by nature of being an instance and helps with code readability (somewhat).

  • You never actually use the i variable in your pairs loops. You can replace these with an underscore to show that you aren’t using the variable.

  • Iterating through all the children of the workspace is a terrible idea and will present you with performance problems. Why not fetch a different way? It seems that it’s your source of error anyway.

This all being said, I’d like to highlight the last point. Your loop here is useless and if your indentation is correct, then it’s improperly written as well. I would advise you perform a direct search for the model you require over checking every single instance in the Workspace.

VehicleDB.Search.MouseButton1Click:Connect(function (playerWhoClicked)
    local plateID = VehicleDB.plateID.Text
    local playerCar = workspace:FindFirstChild(PName.Name.."'s Car")

    if not playerCar then
        local newCar = whatever -- newCar isn't defined...?
        newCar.Name = player.Name.."'s Car"
        local id = create_id(5)
        newCar.PlateNumbers.Value = id
        newCar.Body.Plate.PlateGUI.Frame.PlateID.Text = id

        newCar.Parent = workspace
        newCar:MakeJoints() -- why?
    end
end)

Your code looks somewhat awkward so I don’t know if the code I wrote actually helps solve your problem. Case in point of me providing that code sample: a loop is completely unnecessary in this scenario. Get rid of it.

2 Likes

I don’t know if you noticed, but I have tried options including adding prints, checking console, asking friends, etc etc.

I personally don’t care about getservice and getplayers etc etc. So, I’ll use em whenever it’s needed.

As for the i variable in my pairs loops, I can also just ignore the i.

Iterating all the workspace is not a good idea, you’re correct. I just tend to overthink things.

The newCar isn’t defined in the given code because it’s not important to the problem.

thank you for your help tho

correction: that script didn’t work

Updated the post with a bit more info. This way someone can actually help.

Still, there isn’t enough needed information. You should include the entire script as the script you included will return an error due to the lack of ends

I thought it’d just confuse the heck out of the users, as it’s a LONG, and ANNOYING script

Nah don’t worry, the more info the better. I’d say you’ll have a higher chance of your problem being solved by including the entire script

Aight, I’ve updated the post. At the bottom you’ll see the full code involving the problem.

correct…?

Are both the scripts serverside? Because if the car is created client side and the car searching algorithm is server side then the car would never be found.

I came up with a solution, but then I read the code in the dropdowns and realized I might be misunderstanding the problem and created a second one. Here are the two possible solutions I came up with. Maybe neither of them are the correct solution but there could be something in there that might correct a typing error (or misuse of functions) not noticed by the computer.

Solution 1

You’re using the MouseButton1Click function, which senses when a Gui button is clicked- so I am assuming this is a LocalScript, since, as you probably know, Server Scripts don’t work with Screen Guis.

Since you are using a LocalScript, you are only making changes to the client. If you use a LocalScript, the car’s name will change in the client, which doesn’t sound very useful to me. You’ll want to do this in a Server Script, which will work in the Workspace or ServerScriptService.

To tell the server when to update the car’s name, you’ll want to use Remote Events, which work well when put in Replicated Storage. If you haven’t used Remote Events before, here’s an example of what you might want:

First, you’ll want to put a folder in Replicated Storage called ‘Remotes’ or something, so you can seperate the Remote Events from other things you might store in there. In the folder, you’ll put a remote event with a variable-type name, like ‘carName’, so it will be easy to call when you need it.

This code would go in the LocalScript:

local RS = game:GetService("ReplicatedStorage")
local carName = RS.Remotes:WaitForChild("carName")

VehicleDB.Search.MouseButton1Click:connect(function()
local plateID = VehicleDB.plateID.Text
for _, player in pairs(game.Players:GetPlayers()) do
if findCar(player) then -- It would be good to use a function here
print("Got player's car")
else
carName:FireServer()
end
end)

local findCar = function(player)
local car = workspace.Cars:FindFirstChild(player.Name, "'s Car") -- You should also have a folder in the Workspace for cars
if car then
return car
end
end

Now, here’s the Server Script:

RS = game:GetService("ReplicatedStorage")
carName = RS.Remotes:WaitForChild("carName")
car = game:GetService("ServerStorage"):WaitForChild("Car") -- define the newCar variable here, this is just an example.

carName.OnServerEvent:connect(function(player)
local newCar = car:Clone()
newCar.Parent = workspace.Cars
newCar:MakeJoints()
local id = createId(5)
newCar.Name = player.Name, "'s Car"
newCar.PlateNumbers.Value = id
end)
Solution 2

This might not be a solution, but it’s good for organizing anyway. The player has a property called UserId, which you could use instead of the create_id function. Here’s an example in the car’s script:

newCar.Parent = workspace
newCar:MakeJoints()
local id = player.UserId
newCar.PlateNumbers.Value = id
newCar.Body.Plate.PlateGUI.Frame.PlateID.Text = id
2 Likes

Solution 1 seems like it would work, however I am not sure how this function works…

How do I get the returned car variable?

Would I just use car.body etc etc? And ignore the red error mark?

If you’re saying you want to use the Remote Event to return the car, you just send it back to the server with carName:FireClient and carName.OnClientEvent.

If you mean you don’t understand the findCar function, here’s a walkthrough:

for _, player in pairs(game.Players:GetPlayers()) do
    if findCar(player) then -- this runs the function, and if the function returns with the car, it passes as true
        print("Got player's car")
    else
        carName:FireServer() -- if the function returns without the car, we'll need to create a new one.`
    end
end)

local findCar = function(player) -- Create the function
    local car = workspace.Cars:FindFirstChild(player.Name, "'s Car") -- using the FindFirstChild function will return nil if there is no car, but return with the car if there is a car.
    if car then -- now, we are checking whether the function returned with a car or without
        return car -- return the car to the function
    else
        return false -- do this part if you're getting an error, I haven't tested it so I'm not sure.
    end
end

If you still get errors, try making sure nothing returns nil, because for some reason Roblox can’t seem to handle returning nil values as false?

Well I need to get items inside the car object, but I don’t know how to get the variable for the car. It turns up as an red mark if I put ‘car.whatever’. So, I just don’t know how returning functions work…?

the findCar function returns the car, so to get items inside the car, you could use a variable to hold the car so you don’t have to call the function multiple times.

for _, player in pairs(game.Players:GetPlayers()) do
    local playersCar = findCar(player) -- this variable holds the car
    if playersCar then
        print("Got player's car")
    else
        carName:FireServer() -- if the function returns without the car, we'll need to create a new one.
        playersCar = findCar(player) -- now, the playersCar variable should be able to find and set to the new car.
    end
    -- Down here is where you can do your stuff with the car.
    -- You might need to use more remote events to change things in the server
    -- For example, you might need something like:
    RS.Remotes:WaitForChild("applyProperties"):FireServer(playersCar) -- Of course you'd create a variable for the remote at the top of the script
end)
1 Like