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

How to assign an int value based on a model int value when a player joins the server?

Asked by 10 years ago

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?

0
Can you add some print statements to help determine if the code is reaching the if statements? Merely 2122 — 10y
0
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) Tytanlore 15 — 10y
0
It's possible the player hasn't spawned yet when the script runs. Try local playerModel = player.Character or player.CharacterAdded:wait() Merely 2122 — 10y
0
The player is not teleporting to the place I specified, however, the code is reaching the last if statement (if player.CharacterAdded:wait()) Tytanlore 15 — 10y

Answer this question