Need help with fixing this plot script

im trynna fix this issue this is script in workspace, where there is a string value in each island, but the player can put their name on each island, which is not what i want. i want their name only on the first island they touched, and i also need to make sure, that someone else cant just go to someones island and claim it.
script:

local plr = game:GetService("Players").LocalPlayer
local islands = game:GetService("Workspace").Islands.Islands
local island = islands:GetChildren()
local freeIslands = {}
for i, island in pairs(islands:GetChildren()) do
	if island.Occupant.Value == nil then
		table.insert(freeIslands, island)
	end
end

for i,v in pairs(island) do
	v.Touched:Connect(function(otherPart)
		local character = otherPart:FindFirstAncestorOfClass("Model")
		if character then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid then
				local plr = game.Players:GetPlayerFromCharacter(character);
				v.Occupant.Value = plr.Name
			end
		end
	end)
end

screenshots of one of the issues:


see? the players name can be on all the islands at once.
HELP

If you check the islands occupant value is nil after touching this should prevent it being reassigned. (Sorry about formatting)

for i,v in pairs(island) do
	v.Touched:Connect(function(otherPart)
          if island.Occupant.Value == nil then
		local character = otherPart:FindFirstAncestorOfClass("Model")
		if character then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid then
				local plr = game.Players:GetPlayerFromCharacter(character);
				v.Occupant.Value = plr.Name
			end
		end
            end
	end)
end

Depending on your setup. You could also disconnect the touched event if you don’t actually need it again.

1 Like

i got this error with your code:
Workspace.Scripts.PlotSystem:13: attempt to index nil with 'Value'

Here’s something that will work

for i,v in pairs(island) do
	v.Touched:Connect(function(otherPart)
		local character = otherPart:FindFirstAncestorOfClass("Model")
		if character then
			-- check if player already claimed an island
			for i, v in pairs(islands:GetChildren()) do
			     if v.Occupant.Value == character.Name then
			          return -- stop if name is found
			     end
			end
			-- the rest of this code will not run if function returns
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid then
				local plr = game.Players:GetPlayerFromCharacter(character);
				v.Occupant.Value = plr.Name
			end
		end
	end)
end
2 Likes

lol yeah should be v.Occupant.Value on the 3rd line instead of island.Occupant.Value
@Auxicon 's is better though if the players are only claiming one island.

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