How do I make it so that these npcs can only be interacted with by owner of plots.plot1
local RunService = game:GetService("RunService")
local Current_NPC
local MIN_TIME = 5
local MAX_TIME = 15
local debounce = false
local Spawn_Part = workspace.Plots:FindFirstChild("Plot1"):WaitForChild("SpawnPart")
local Move_To_Part = workspace.Plots:FindFirstChild("Plot1"):WaitForChild("MoveToPart")
local table = game:GetService("ReplicatedStorage").NPCS:GetChildren()
local NPC = table[math.random(1,#table)]
local function spawnNPC(NPC)
local clone = NPC:Clone()
clone.Parent = workspace
clone:SetPrimaryPartCFrame(Spawn_Part.CFrame + Vector3.new(0,5,0))
clone:FindFirstChild("Humanoid"):MoveTo(Move_To_Part.Position)
Current_NPC = clone
coroutine.wrap(function()
local connection
connection = RunService.Heartbeat:Connect(function()
if clone:IsDescendantOf(workspace) then
return
end
Current_NPC = nil
connection:Disconnect()
end)
end)()
end
spawnNPC(NPC)
RunService.Heartbeat:Connect(function()
if debounce then
return
end
debounce = true
if Current_NPC then
debounce = false
return
end
task.wait(math.random(MIN_TIME, MAX_TIME))
NPC = table[math.random(1,#table)]
spawnNPC(NPC)
debounce = false
end)
What if you made an objectValue or stringValue inside the NPC which holds the PlayerObject or Playername, then if the NPC gets interacted with and the player to interact is the one in the NPC’s value it does something.
local PP = game:GetService("Workspace").Plots.Plot1["ProximityPromptV2.2"].ProximityPrompt -- Assuming the proximity prompt's inside that folder.
local Owner = PP.Parent.Parent:FindFirstChild("Owner").Value -- Assuming there's a stringvalue containing the owner's username inside Plot1.
for _, v in pairs(game:GetService("Workspace"):GetDescendants()) do
if v:IsA("Humanoid") then
if (script.Parent.Position-v.Parent.HumanoidRootPart.Position).Magnitude <= 5) then
if v.Parent.Name ~= Owner then
PP.Enabled = false
else
PP.Enabled = true
end
end
end
end
local Library = {
ID_1 = {
Price = 100,
FolderName = "ChickenCoop",
DisplayName = "Buy Chicken Coop"
},
ID_2 = {
Price = 200,
FolderName = "Barn",
DisplayName = "Buy A Barn"
}
}
local OwnerShip = {}
local ServerE = game:GetService("ServerScriptService"):FindFirstChild("TycoonHandler"):WaitForChild("Server")
local ClientE = game:GetService("ReplicatedStorage"):WaitForChild("Client")
for i,v in pairs(game:GetService("Workspace"):FindFirstChild("Plots"):GetChildren()) do
for i,v in pairs(game:GetService("Workspace"):FindFirstChild("Plots"):FindFirstChild("Plot"..i):FindFirstChild("Buttons"):GetChildren()) do
local ButtonData = Library["ID_"..v.ButtonHandler.ButtonId.Value]
if ButtonData then
local Clone = script:FindFirstChild("ButtonFloat"):Clone()
Clone.Frame.ButtonName.Text = ButtonData.DisplayName
Clone.Frame.ButtonPrice.Text = ButtonData.Price.." $"
Clone.Parent = v
end
wait()
end
wait()
end
local DSS = game:GetService("DataStoreService")
local ProgressStore = DSS:GetDataStore("Progress")
ServerE.Event:Connect(function(Command, Data)
local Player = game:GetService("Players"):FindFirstChild(Data.Player)
if Player then
if Command == "BuyItem" then
local OwnTycoon = OwnerShip["Player_"..Player.UserId] -- Check if player own a tycoon
local HasMoney = Player:FindFirstChild("leaderstats"):WaitForChild("Gold").Value >= Library["ID_"..Data.ButtonId].Price -- Check if player has enough money
local Progress = false
local TheirTycoon = false
if OwnTycoon then
Progress = table.find(OwnerShip["Player_"..Player.UserId].Progress, Data.ButtonId) -- Check if player doesnt already own the object
TheirTycoon = Data.Tycoon == "Plot"..OwnerShip["Player_"..Player.UserId].TycoonId
end
if OwnTycoon and HasMoney and not Progress and TheirTycoon then
for i,v in pairs(game:GetService("Workspace"):FindFirstChild("Plots"):FindFirstChild("Plot"..OwnTycoon.TycoonId):FindFirstChild("Buttons"):GetChildren()) do
if v.ButtonHandler.ButtonId.Value == Data.ButtonId then
v:Destroy()
end
end
for i,v in pairs(game:GetService("ServerStorage"):FindFirstChild("PlotsBuyables"):FindFirstChild("Plot"..OwnTycoon.TycoonId):FindFirstChild("Buyables"):FindFirstChild(Library["ID_"..Data.ButtonId].FolderName):GetChildren()) do
v:Clone().Parent = game:GetService("Workspace"):FindFirstChild("Plots"):FindFirstChild("Plot"..OwnTycoon.TycoonId):FindFirstChild("Buyables")
end
Player:FindFirstChild("leaderstats"):WaitForChild("Gold").Value = Player:FindFirstChild("leaderstats"):WaitForChild("Gold").Value - Library["ID_"..Data.ButtonId].Price
table.insert(OwnerShip["Player_"..Player.UserId].Progress, Data.ButtonId)
elseif not HasMoney then
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = game:GetService("Players"):GetUserThumbnailAsync(Player.UserId, thumbType, thumbSize)
local NotifData = {
Title = "Not enough money!",
Text = "Progress: "..Player:FindFirstChild("leaderstats"):WaitForChild("Gold").Value.."$/"..Library["ID_"..Data.ButtonId].Price.."$",
Icon = content,
Duration = 5
}
ClientE:FireClient(Player,"Notification", NotifData)
elseif not TheirTycoon then
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = game:GetService("Players"):GetUserThumbnailAsync(Player.UserId, thumbType, thumbSize)
local NotifData = {
Title = "This isn't your tycoon!",
Text = "Try buying this on your own plot.",
Icon = content,
Duration = 5
}
ClientE:FireClient(Player,"Notification", NotifData)
else
-- player already ownes it or doesnt own a tycoon
end
elseif Command == "PlayerClaimedTycoon" then
local TycoonNumb = Data.Tycoon
local Prog = ProgressStore:GetAsync(Player.UserId)
if not Prog then
Prog = {}
elseif tonumber(Prog) then
Prog = {}
end
--[[
if Player.UserId == 1754236469 or Player.UserId == 281762843 then
Prog = {}
end
]]--
local AlreadyOwned = OwnerShip["Player_"..Player.UserId]
if TycoonNumb and not AlreadyOwned then
OwnerShip["Player_"..Player.UserId] = {
TycoonId = TycoonNumb,
Progress = Prog
}
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("OwnerDoor"):FindFirstChild("MainDoor"):Destroy()
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("OwnerDoor"):FindFirstChild("NameDoor"):FindFirstChild("NameGUI"):FindFirstChild("TextLabel").Text = Player.Name
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("Buyables"):ClearAllChildren()
for i = 1, #OwnerShip["Player_"..Player.UserId].Progress do
if Library["ID_"..i] then
local Clone = game:GetService("ServerStorage"):WaitForChild("PlotsBuyables"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("Buyables"):FindFirstChild(Library["ID_"..i].FolderName):Clone()
if Clone then
Clone.Parent = game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("Buyables")
else
print("Something went wrong spawning the tycoon")
end
end
end
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("Buttons"):ClearAllChildren()
for i,v in pairs(game:GetService("ServerStorage"):FindFirstChild("PlotsBuyables"):FindFirstChild("Plot"..OwnerShip["Player_"..Player.UserId].TycoonId):FindFirstChild("Buttons"):GetChildren()) do
if not table.find(OwnerShip["Player_"..Player.UserId].Progress, v.ButtonHandler.ButtonId.Value) then
v:Clone().Parent = game:GetService("Workspace"):FindFirstChild("Plots"):FindFirstChild("Plot"..OwnerShip["Player_"..Player.UserId].TycoonId):FindFirstChild("Buttons")
end
end
for i,v in pairs(game:GetService("Workspace"):FindFirstChild("Plots"):FindFirstChild("Plot"..TycoonNumb):FindFirstChild("Buttons"):GetChildren()) do
local ButtonData = Library["ID_"..v.ButtonHandler.ButtonId.Value]
if ButtonData then
local Clone = script:FindFirstChild("ButtonFloat"):Clone()
Clone.Frame.ButtonName.Text = ButtonData.DisplayName
Clone.Frame.ButtonPrice.Text = ButtonData.Price.." $"
Clone.Parent = v
end
wait()
end
else
return
-- Probably send an event to the player that something went wrong
-- or not no one rlly cares
end
end
else
return print("Invalid player")
end
end)
local Plrs = game:GetService("Players")
Plrs.PlayerRemoving:Connect(function(P)
if OwnerShip["Player_"..P.UserId] then
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..OwnerShip["Player_"..P.UserId].TycoonId):FindFirstChild("Buttons"):ClearAllChildren()
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..OwnerShip["Player_"..P.UserId].TycoonId):FindFirstChild("Buyables"):ClearAllChildren()
game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..OwnerShip["Player_"..P.UserId].TycoonId):FindFirstChild("OwnerDoor"):FindFirstChild("NameDoor"):FindFirstChild("NameGUI"):FindFirstChild("TextLabel").Text = "CLAIM PLOT"
game:GetService("ServerStorage"):WaitForChild("PlotsBuyables"):FindFirstChild("Plot"..OwnerShip["Player_"..P.UserId].TycoonId):FindFirstChild("Door"):Clone().Parent = game:GetService("Workspace"):WaitForChild("Plots"):FindFirstChild("Plot"..OwnerShip["Player_"..P.UserId].TycoonId):FindFirstChild("OwnerDoor")
ProgressStore:SetAsync(P.UserId, OwnerShip["Player_"..P.UserId].Progress)
OwnerShip["Player_"..P.UserId] = nil;
end
end)