String to instance conversion

so how would i convert the string into an instance in this script: (line 4 and line 6)

local players = game:GetService("Players")
local function onPlayerAdded(player)
    local playerCount = #game.Players:GetPlayers()
    local playerinstance = "workspace.Player" .. playerCount
    print(playerinstance)
    local position = playerinstance
    print(position)
    position.Value = player.Name
    position.Parent = workspace
end

local function onPlayerRemoving(player)
    local playerinstance = "workspace.Player" .. playerCount
    local position = playerinstance
    for _, _player in ipairs(players:GetPlayers()) do
        local _position = _player.Position
        if _position.Value > position.Value then
            _position.Value -= 1
        end
    end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
1 Like

To convert a string into an instance, you need to use the following:

local playerInstance = workspace.Player:GetChildren()[playerCount]
1 Like

ill tell you if it works!!!

1 Like

it didn’t work im still getting the same error:

Player is not a valid member of Workspace "Workspace"
1 Like

woops! I forgot to add something.

local playerInstance = game.Players:GetPlayers()[playerCount].Character

hope this works!

1 Like

the error i got from your new script was

Workspace.assignvalue:4: invalid argument #2 (string expected, got Instance)
1 Like

Edited the line, try it again and let me know if it works or not

i don’t think you understand what im trying to do…

my original line of code:

 local playerinstance = "workspace.Player" .. playerCount

i am trying to make it be workspace.Player and if there is one player in the server it would be workspace.Player1

but roblox says its a string thats why im trying to convert it

1 Like

are you trying to find the player through the workspace?

You can always use loadstring for that but it’s honestly the least efficient option out there.

You could try:

local playerinstance = workspace:FindFirstChild("Player"..playerCount)

However if Player…playerCount (example: Player1) doesn’t exist, the entire playerinstance will turn into nil

it will always exist… it’s a instance in workspace anyways thank you i’ve been trying for hours

1 Like

Did it work? If it did happen to work, I’d appreciate it being marked as solved so that others could directly see the solution

already did thanks bro!!!

2 Likes

but now i have another problem

local players = game:GetService("Players")
local function onPlayerAdded(player)
	local playerCount = #game.Players:GetPlayers()
	local playerinstance = workspace:FindFirstChild("Player"..playerCount)
	print(playerinstance)
	local position = playerinstance
	print(position)
	position.Value = player.Name
	position.Parent = workspace
end

local function onPlayerRemoving(player)
	local playerCount = #game.Players:GetPlayers()
	local playerinstance = workspace:FindFirstChild("Player"..playerCount)	
	local position = playerinstance
	for _, _player in ipairs(players:GetPlayers()) do
		local _position = _player.Position
		if _position.Value > position.Value then
			_position.Value -= 1
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)

the function OnPlayerRemoving what i want it to do is if player1 leaves Player2 turns into player1 cause it’s their usernames in the value

1 Like

You could check if Player1 exists and if it doesn’t then have Player2 turn into Player1, this however should be asked in another question since the original question has been solved

can you still help??? or do you want me to make another and message you the link

1 Like

In another question I can help, yeah. But before that try putting
playerinstance = workspace:FindFirstChild("Player".._position.Value)
right after _position.Value -= 1

As a side note, I have to go

that didn’t work but will do make another topic

1 Like