Cannot Claim More Than One Building Area

Hello, I am in need of assistance so that you cannot claim more than one private building area.

The pads are what indicates if you have access to place inside of the transparent blocks area. I am trying to make it so you cannot claim more than one pad, could anyone help me?

https://streamable.com/ax4953

2 Likes

So there a few different ways you can achieve this but the way I would do it is have a table in a script and if the player stands on a pad to claim a building area then it looks through the table and using a if statement make it so if the player username who touched the pad is not in the table then let them claim the pad and add there username to the table but if it is in the table then just do not run the claiming script and either do nothing or make some type of UI come up saying they can’t clam any more then one or something.

Could you possibly provide me with some code to work with?

1 Like

Yes let me quickly write the basic idea of how it will work.

1 Like

:white_check_mark: Alrighty, see ya when it’s ready. :slightly_smiling_face:

1 Like

If you are using OOP then you can make something like this

local padModule = require(someModuleHere)
local somePart = workspace.SomePart

local padHandler = padModule.new()

somePart.Touched:Connect(function() -- get the player using hit and add debounce
  padHandler:AssignPad(plr)
end)

-- The script below will be in the module
function module:AssignPad(plr)
  if self.AssignedPads[plr.Name] then
      return
  end

  self.AssignedPads[plr.Name] = plr
   -- Do more stuff here
end

now you can check if the player is in the table and if the player is not in the table then assign them with a new pad

I’m really new to scripting, can you elaborate on what OOP is?

1 Like

I will share my code with you two and see if there is a way y’all could help.

You can refer to @LifeDigger 's reply to know what it is in detail

Here is a short explanation: Object Oriented Programming or OOP for short is basically creating Instances and giving them custom properties using metatables and metamethods (refer to LifeDigger’s post for examples and replies)

Alright, if I share the piece(s) of code with you, could you make an attempt to help?

1 Like

Please do share I can try my best to help

local players = game:GetService(“Players”)
local replicatedStorage = game:GetService(“ReplicatedStorage”)

local claims = workspace:WaitForChild("[Claim Private Areas]")
local areas = workspace:WaitForChild("[Private Building Areas]")

local function newClaim(claim)
local area = areas:WaitForChild(claim.Name)
local owner = nil

claim.Touched:Connect(function(part)
	if not owner then
		local player = players:GetPlayerFromCharacter(part.Parent)

		if player then
			owner = player
			local permissions = require(area["[Permissions]"])
			
			permissions[1] = {
				Type = "Player",
				PlayerId = player.UserId,
				PlayerName = player.Name
			}
			
			replicatedStorage.UpdatePermissions:FireAllClients(area, player)
			
			claim.Color = Color3.new(1, 0, 0)
			player.AncestryChanged:Wait()
			owner = nil
			claim.Color = Color3.new(0, 1, 0)
			
			replicatedStorage.UpdatePermissions:FireAllClients(area, nil)
		end
	end
end)

end

for i, claim in pairs(claims:GetChildren()) do
coroutine.wrap(newClaim)(claim)
end

claims.ChildAdded:Connect(newClaim)

Please do share the code I can try my best to help

You can store a StringValue inside of the pad holding the occpuant. If the player that touched the pad owns it than you can return else StringValue.Value = hit.Parent.Name.

local players = game:GetService(“Players”)
local replicatedStorage = game:GetService(“ReplicatedStorage”)

local claims = workspace:WaitForChild("[Claim Private Areas]")
local areas = workspace:WaitForChild("[Private Building Areas]")

local function newClaim(claim)
local area = areas:WaitForChild(claim.Name)
local owner = nil

claim.Touched:Connect(function(part)
	if not owner then
		local player = players:GetPlayerFromCharacter(part.Parent)
                if player:FindFirstChild("OwnsPad") then
                     return
                end
                local ownerTag = Instance.new("BoolValue")
                ownerTag.Name = "OwnsPad"
                ownerTag.Parent = plr
		if player then
			owner = player
			local permissions = require(area["[Permissions]"])
			
			permissions[1] = {
				Type = "Player",
				PlayerId = player.UserId,
				PlayerName = player.Name
			}
			
			replicatedStorage.UpdatePermissions:FireAllClients(area, player)
			
			claim.Color = Color3.new(1, 0, 0)
			player.AncestryChanged:Wait()
			owner = nil
			claim.Color = Color3.new(0, 1, 0)
			
			replicatedStorage.UpdatePermissions:FireAllClients(area, nil)
		end
	end
end)
end

for i, claim in pairs(claims:GetChildren()) do
coroutine.wrap(newClaim)(claim)
end

claims.ChildAdded:Connect(newClaim)

Try this code this should work (make sure to delete the owner tag from the player once the plot is not owned by the player anymore)

What do you mean delete the owner tag?

the code also does not work unfortunately.

Hello! Where would I add the else StringValue.Value = hit.Parent.Name . in the code?

here I create a bool value inside the player which tells us that the player owns a pad so you need to delete it once the player does not own the plot anymore