Sorry if the title made no sense, english is not my native language.
What I'm doing is, I have a Model on Workspace called Factories, and inside, there's 8 models that are called Factory. I want to loop through each one, get the IntValue and StringValue from them, and depending on the StringValue value, assign the Factory IntValue to the player who just joined.
Workspace > Factories > Factory > FactoryNumber and OwnerName
this is my script so far, and it's not working, no errors are thrown on the console:
EDIT: it is inside the function that is connected to the PlayerAdded event.
for _,factory in pairs(game.Workspace.Factories:GetChildren()) do
local isOwnedBy = factory.OwnerName
local factoryNumber = factory.FactoryNumber
local spawnPos = factory.SpawnPoint.Position
local playerModel = Workspace:FindFirstChild(player.Name)
if isOwnedBy.Value == "" and player.FactoryNumber.Value == 0 then
player.FactoryNumber.Value = factoryNumber.Value
isOwnedBy.Value = player.Name
if playerModel:IsA("Model") then
playerModel.Character.Torso.CFrame = spawnPos
end
end
end
Is there something wrong with my script?

4 comments
Can you add some print statements to help determine if the code is reaching the if statements?
Apparently, Data Persistence was keeping my code from running. Now I only have an error - teleporting the player. This is the error: attempt to index local 'playerModel' (a nil value)
It's possible the player hasn't spawned yet when the script runs. Try local playerModel = player.Character or player.CharacterAdded:wait()
The player is not teleporting to the place I specified, however, the code is reaching the last if statement (if player.CharacterAdded:wait())