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

Character doesn't spawn to brick when value matches spawn name?

Asked by 5 years ago
Edited 5 years ago

So, basically a run down, I created a value in player that assigns a string value to how ever many players there is in the game, for example when there is 3 people in the game, whenever a new player joins, the string will automatically be changed to "Player 4". I also have spawn locations that are named from "Player 1" to "Player 7", and if the string value matches the name of one of the spawn locations, that player would be spawned to that specific spawn brick. Thats the problem: the character won't spawn to the brick. Any help? Normal server script in server script service:

game.Players.PlayerAdded:Connect(function(player)
    local playernum = Instance.new("StringValue")
    playernum.Parent = player
    playernum.Name = "Player"
    local players = game.Players:GetPlayers()
    for _, v in pairs(players) do
        playernum.Value = "Player "..#players
    end
    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
        local lowertorso = char:WaitForChild("LowerTorso")
        local spawnlocations = game.Workspace:WaitForChild("BloxopolyBoard"):WaitForChild("SpawnLocations"):GetChildren()
        if humanoid and lowertorso then
            for _, v in pairs(spawnlocations) do
                if playernum.Value == v.Name then
                    lowertorso.CFrame = CFrame.new(v.Name.Position)
                end
            end
        end
    end)
end)

With no changes i get the error: ServerScriptService.Script:16: bad argument #1 to 'new' (Vector3 expected, got nil)

0
v.Position, not v.Name.Position. Strings don't have a Position property, and it didn't work for that reason. User#19524 175 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Position is a Vector3 value of a part, not a CFrame which your using. Use v.Name.CFrame.Position as Position returns the position of the CFrame. Its also suggested to use the player's HumanoidRootPart as its available for R6 and R15.

game.Players.PlayerAdded:Connect(function(player)
    local playernum = Instance.new("StringValue")
    playernum.Parent = player
    playernum.Name = "Player"

    playernum.Value = "Player "..#players

    player.CharacterAdded:Connect(function(char)
        local humanoid = char:FindFirstChild("Humanoid")
        local lowertorso = char:FindFirstChild("HumanoidRootPart")
        local spawnlocations = game.Workspace:WaitForChild("BloxopolyBoard"):WaitForChild("SpawnLocations"):GetChildren()
        if humanoid and lowertorso then
            for _, v in pairs(spawnlocations) do
                if playernum.Value == v.Name then
                    lowertorso.CFrame = CFrame.new(v.CFrame.Position) -- im not sure if "Name" is a child of the spawnlocation or your accessing the property.
                end
            end
        end
    end)
end)

Your also just using a generic for loop for no reason, as your getting the length of a table from :GetPlayers() instead of actually using it and you should use :FindFirstChild() instead of using :WaitForChild() for checking if something exists.

Ad

Answer this question