here i have a script that assigns you to a plot (part) when you join the game. I made a button in startergui, when its pressed, it fires a remote event. Then i have a script in ServerScriptService that receives it. That script is the one will all the code that assigns a player a plot when he join the game. When you press the button, it teleports you to your assigned plot, but if i try it in a server with 2 players, it teleports one of the two on a plot that isnt theirs. If you know how to fix this please help me i would appreciate it. Here is the script:
local firstocuped = false
local thirdocuped = false
local fourthocuped = false
local fifthtocuped = false
local sixthocuped = false
local seventhocuped = false
local eighthocuped = false
local ninthocuped = false
local tenthocuped = false
local TpToHouseEvent = game.ReplicatedStorage.TpToHouseEvent
game.Players.PlayerAdded:Connect(function(player)
wait(0.7)
repeat wait(0.1)
until player.Character ~= nil and player.Character:FindFirstChild("HumanoidRootPart")
local data = Instance.new("IntValue")
data.Name = "Data"
data.Parent = player
local data2 = Instance.new("ObjectValue")
data2.Name = "AssignedPlot"
data2.Parent = data
local randomNumber = math.random(1, 10)
local plot = nil
if randomNumber == 1 then
if firstocuped == false then
firstocuped = true
plot = game.Workspace.Plots.Plot1
end
else
if secondocuped == false then
secondocuped = true
plot = game.Workspace.Plots.Plot2
else
if thirdocuped == false then
thirdocuped = true
plot = game.Workspace.Plots.Plot3
else
if fourthocuped == false then
fourthocuped = true
plot = game.Workspace.Plots.Plot4
else
if fifthtocuped == false then
fifthtocuped = true
plot = game.Workspace.Plots.Plot5
else
if sixthocuped == false then
sixthocuped = true
plot = game.Workspace.Plots.Plot6
else
if seventhocuped == false then
seventhocuped = true
plot = game.Workspace.Plots.Plot7
else
if eighthocuped == false then
eighthocuped = true
plot = game.Workspace.Plots.Plot8
else
if ninthocuped == false then
ninthocuped = true
plot = game.Workspace.Plots.Plot9
else
if tenthocuped == false then
tenthocuped = true
plot = game.Workspace.Plots.Plot10
end
end
end
end
end
end
end
end
end
end
player.Data.AssignedPlot.Value = plot
wait(0.21)
player.Character.HumanoidRootPart.CFrame = plot.CFrame
game.ReplicatedStorage.TpToHouseEvent.OnServerEvent:Connect(function(player)
player.Character.HumanoidRootPart.CFrame = plot.CFrame
end)
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerService = game:GetService("Players")
local Event = ReplicatedStorage:WaitForChild("TpToHouseEvent" ,30)
local PlotsFolder = workspace:WaitForChild("Plots" ,30)
local TablePlots = {}
for i = 1,10 do
table.insert(TablePlots, "Plot"..i)
end
PlayerService.PlayerRemoving:Connect(function(Player)
local Data = Player:FindFirstChild("Data")
local Plot = Data and Data:FindFirstChild("AssignedPlot")
if Plot then
table.insert(TablePlots, tostring(Plot.Value))
end
end)
PlayerService.PlayerAdded:Connect(function(Player)
local Character = Player.Character
local Root = Character and Character:WaitForChild("HumanoidRootPart", 30)
local Data = Instance.new("IntValue", Player)
local Plot = Instance.new("ObjectValue", Data)
local RandomPlot = TablePlots[math.random(1, #TablePlots)]
local SelectedPlot = PlotsFolder:FindFirstChild(RandomPlot)
Data.Name = "Data"
Plot.Name = "AssignedPlot"
if SelectedPlot and Root then
Plot.Value = SelectedPlot
table.remove(TablePlots, RandomPlot)
Root.CFrame = SelectedPlot.CFrame
end
end)
Event.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local Root = Character and Character:WaitForChild("HumanoidRootPart", 30)
local Data = Player:FindFirstChild("Data")
local Plot = Data and Data:FindFirstChild("AssignedPlot")
if Plot and Root then
Root.CFrame = Plot.CFrame
end
end)
MoveTo is not reliable. If there is a roof, it will teleport the character above the roof. And, the character could be facing the wrong way. PivotTo is a better option.
Having worked a lot with plot systems and seeing that mine is actually quite similar to yours in one of my own games, I think I can give you some help to assist you not just with your problem, but also some neat advice for programming.
To drastically simplify this script, we can make use of a for loop to handle the plot-choosing logic, which can be put inside of a picker function to separate the logic apart from the main body of code so that it’s easier to see what part does what.
I see you have some sort of “Data” system to maintain a system that can see what player owns what plot. Personally I prefer to hold the data inside the plots themselves, but if the player version makes more sense to you, go for it.
I took some time to re-write your code based on what I think you’re looking for
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TpToHouseEvent = ReplicatedStorage:WaitForChild("TpToHouseEvent")
local plots = workspace:WaitForChild("Plots")
local function check_assigned(plot)
for _, player in next, Players:GetPlayers() do
local data = player:FindFirstChild("Data")
if data and data.AssignedPlot.Value == plot then
return true
end
end
return false
end
local function choose_plot()
local chance = math.random(1,10)
for i = 1, 10 do
local plot = plots["Plot"..i]
if not check_assigned(plot) and chance == 1 and i == 1 or i ~= 1 then
return plot
end
end
end
--only created a new function here to generalize tp'ing code
local function tp_to_house(character)
character:SetPrimaryPartCFrame(plot.CFrame)
end
game.Players.PlayerAdded:Connect(function(player)
--As a matter of logic, don't parent the data to the player until you have
--finalized its construction.
local data = Instance.new("IntValue")
data.Name = "Data"
local data2 = Instance.new("ObjectValue")
data2.Name = "AssignedPlot"
data2.Parent = data
data.Parent = player
player.Data.AssignedPlot.Value = choose_plot()
player.CharacterAdded:Connect(function(character)
--Only bother with spawning logic whenever the character is loaded
task.wait(0.1) --small wait to ensure any roblox-related movement is handled first
tp_to_house(character)
end)
end)
TpToHouseEvent.OnServerEvent:Connect(function(player)
local character = player.Character
if character and character.PrimaryPart then
tp_to_house(character)
end
end)