I am making a claiming system for a tycoon game, but I am having issues with it.
Below is the script and its error.
Please keep in mind that I am new to Roblox Lua scripting.
The Script
local Plots = game.Workspace.Plots
local function AddedPlayer(player, otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
for _, plot in pairs(Plots:GetChildren()) do
--If the plot is taken
if plot:GetAttribute("Taken") then
continue
end
--If the plot Is not taken
plot:SetAttribute("Taken", true)
plot:SetAttribute("Owner" ..player.UserId)
end
print("This Plot is Occupied by" ..player.Name)
end
end
local function RemovingPlayer(player)
for _, plot in pairs(Plots:GetChildren()) do
if not plot:GetAttribute("Owner") then
continue
end
if player ~= plot:GetAttribute("Owner" ..player.UserId) then
continue
end
plot:SetAttribute("Taken", nil)
plot:SetAttribute("Owner", nil)
end
end
for i, player in Players:GetChildren() do
AddedPlayer(player)
end
game.Players.PlayerAdded:Connect(function(player)
AddedPlayer(player)
end)
game.Players.PlayerAdded:Connect(function(player)
RemovingPlayer(player)
end)
The Error ServerScriptService.AnythingScriptFolder.PlotScript:37: attempt to index nil with 'GetChildren' - Server - PlotScript:37
I did it but
but it’s giving me another error ServerScriptService.AnythingScriptFolder.PlotScript:5: attempt to index nil with 'Parent' - Server - PlotScript:5
the thing is that you don’t need the player character nor the humanoid for that system
does changing the function to this work?
local function AddedPlayer(player)
for _, plot in Plots:GetChildren() do
--If the plot is taken
if plot:GetAttribute("Taken") then
continue
end
--If the plot Is not taken
plot:SetAttribute("Taken", true)
plot:SetAttribute("Owner" ..player.UserId)
end
end
This script should fix the issues you’re experiencing:
-- Tested using a server script in ServerScriptService
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local plots = Workspace:WaitForChild("Plots")
local function onPlayerAdded(player: Player)
for _, plot in plots:GetChildren() do
if plot:GetAttribute("Taken") then continue end
plot:SetAttribute("Taken", true)
plot:SetAttribute("Owner", player.UserId)
print(`The owner of plot {plot} is now {player.UserId}`)
break -- Remember to break the loop once a plot is found!
end
end
local function onPlayerRemoving(player: Player)
for _, plot in plots:GetChildren() do
if plot:GetAttribute("Owner") == player.UserId then
plot:SetAttribute("Taken", nil)
plot:SetAttribute("Owner", nil)
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
for _, player in Players:GetPlayers() do onPlayerAdded(player) end
-- Tested using a server script in ServerScriptService
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local plots = Workspace:WaitForChild("Plots")
local function onPlayerRemoving(player: Player)
for _, plot in plots:GetChildren() do
if plot:GetAttribute("Owner") == player.UserId then
plot:SetAttribute("Owner", nil)
break
end
end
end
Players.PlayerRemoving:Connect(onPlayerRemoving)
for _, plot in plots:GetChildren() do
if plot:IsA("BasePart") then
plot.Touched:Connect(function(otherPart: BasePart)
if plot:GetAttribute("Owner") ~= nil then return end -- Plot is already owned by someone
local model = otherPart:FindFirstAncestorOfClass("Model")
if model == nil then return end
local player = Players:GetPlayerFromCharacter(model)
if player then
plot:SetAttribute("Owner", player.UserId)
print(`The owner of plot {plot} is now {player.UserId}`)
end
end)
end
end
you have to use the Touched event and connect it to the function when its touched
this is an example script (there are comments)
local Players = game:GetService("Players")
local Plots = game.Workspace.Plots
local function AddedPlayer(other :BasePart, plot :BasePart)
local character :Model = other.Parent
if not character:FindFirstChild("Humanoid") then
return -- Exit the function if the touched part isnot a player
end
local player :Player = Players:GetPlayerFromCharacter(character) -- this function returns a player from the character
--same as old code
if not plot:GetAttribute("Owner") then
return
end
if player ~= plot:GetAttribute("Owner" ..player.UserId) then
return
end
plot:SetAttribute("Taken", nil)
plot:SetAttribute("Owner", nil)
end
local function RemovingPlayer(player)
for _, plot in Plots:GetChildren() do
if not plot:GetAttribute("Owner") then
continue
end
if player ~= plot:GetAttribute("Owner" ..player.UserId) then
continue
end
plot:SetAttribute("Taken", nil)
plot:SetAttribute("Owner", nil)
end
end
--Loops through all plots and connects them to the function
for _,plot :Part in Plots do
plot.Touched:Connect(function(hit)
AddedPlayer(hit, plot)
end)
end
Players.PlayerRemoving:Connect(RemovingPlayer)