Why is this code not working?

I’ve got a major issue with this spawn location code…

local Plots = game.Workspace.Plots

local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(player)
for _, plrplot in Plots:GetChildren() do
		if plrplot:GetAttribute('Taken') == true and plrplot:GetAttribute('Owner') == player.UserId then
			player.RespawnLocation = plrplot.SpawnLocation
		end
	end
end)

https://gyazo.com/f8329fefb2740747a3d127cab641a301

For some reason the game is spawning the player on the wrong platform even tho i have loop to check for each plot and its attributes that match then set the spawn location and when the player respawns, it changes the spawn location.
Please I need immediate help i cant figure out what i did wrong

1 Like
local Plots = game.Workspace.Plots
local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(player)
    for _, plrplot in ipairs(Plots:GetChildren()) do
        if plrplot:GetAttribute('Taken') == true and plrplot:GetAttribute('Owner') == player.UserId then
            player.RespawnLocation = plrplot.SpawnLocation.Position 
            break  -- Exit the loop once good plot found
        end
    end
end)
1 Like


still happens even after the edit

1 Like

Did you checked if you put all the spawn locations on the right position?

yeah, all the spawns are inside the plots themselves

1 Like

Weird, that’s very strange, try printing if he’s spawning in the right position, like, print where his x and y spawned, just for testing.

Try this script

local Plots = game.Workspace.Plots

local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(player)
for _, plrplot in Plots:GetChildren() do
      local SpecifcPlrPlot = plrplot
		if SpecifcPlrPlot:GetAttribute('Taken') == true and SpecifcPlrPlot:GetAttribute('Owner') == player.UserId then
			player.RespawnLocation = SpecifcPlrPlot.SpawnLocation
		end
	end
end)


i tried adding a print to see if it even detects the attributes, and it seems like it doesn’t

1 Like

so, that explains it, for some reason it doesn’t even find the spawnlocation

https://gyazo.com/b5744865d21d7305774d16bb49e0453d

any clue on how i would be able to fix it? because it should stereotypically find it, it has all the checks it needs

1 Like

Tbh, i’m not an expert on scripting but i’m gonna try

Can you show a bit of your explorer, like the attributes and other stuff?

What’s the script that sets the attribute for Taken and Owner?


script:WaitForChild('CreatePlot').Event:Connect(function(Player, ItemIdsTable)
	for _, plot in Plots:GetChildren() do
		
		if plot:GetAttribute('Taken') then continue end
		plot:SetAttribute('Taken', true)
		plot:SetAttribute('Owner', Player.UserId)
		
		local ItemsFolder = Instance.new('Folder')
		ItemsFolder.Name = 'Items'
		ItemsFolder.Parent = plot
		

this is the code that assigns the attributes to the plot
image
and this is the explorer

they are made in 2 different scripts, but i believe they could be made in the same one

Can’t you set the respawn location in the same script you set the userID (Owner attribute)?

1 Like

OH YEAH! THAT ACTUALLY DID IT :sob: thank you! that completely slipped my mind

1 Like


thank you so much bro, it works with multiple people as well!

2 Likes

Firstly don’t use pairs for itterating through a folder

local Plots = game.Workspace.Plots
local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(player)
    for _, plrplot in Plots do
        local Plot = plrplot
        if plrplot:GetAttribute('Taken') == true and plrplot:GetAttribute('Owner') == player.UserId then
            player.RespawnLocation = plrplot.SpawnLocation.Position 
          print("RUN")
            break  -- Exit the loop once good plot found
        end
    end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.