This script only works for one player?

if i try to play test this script with two people inside of roblox studio it only works for one person, like only Player1 is able to fly. Its inside of starterplayerscripts, also im using the ZonePlus module by foreverHD incase im just doing something wrong for that

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local ZoneModule = require(ReplicatedStorage.Modules:WaitForChild("Zone"))
local FlyModule = require(ReplicatedStorage.Modules:WaitForChild("FlyModule"))

local player = game:GetService("Players").LocalPlayer

local function waitForPlayerGarden(player)
	local gardensFolder = workspace:WaitForChild("PlayerGardens")
	local gardenName = player.Name.."_Garden"
	
	local timeout = 10
	local timer = 0
	while not gardensFolder:FindFirstChild(gardenName) and timer < timeout do
		
		warn("Waiting for "..player.Name.."'s garden")
		
		task.wait(0.1)
		timer += 0.1
	end
	
	return gardensFolder:FindFirstChild(gardenName)
end

local function setupZones(playerGarden)

	local gardenName = player.Name .. "_Garden"
	local gardensFolder = game.Workspace:WaitForChild("PlayerGardens")
	
	playerGarden:WaitForChild("Zone"):SetAttribute("Owner", player.Name)
	
	local playerGui = player:WaitForChild("PlayerGui")
	local screenGui = playerGui:WaitForChild("ScreenGui")
	local zoneFrame = screenGui:WaitForChild("ZoneFrame")
	local zoneLabel = zoneFrame:WaitForChild("ZoneLabel")
	
	if playerGarden then
		for _, container in pairs(playerGarden:GetChildren()) do
			if container:IsA("Folder") and container.Name == "Zone" and container:GetAttribute("Owner") == player.Name then
				local spacePart = container:FindFirstChild("Space")
				local Zone = ZoneModule.new(container)
				
				print(gardenName.." has been initialized")
				
				if spacePart then
					print("SpacePart found for "..gardenName)
					
					Zone.playerEntered:Connect(function()
						print("Player entered:", spacePart.Name)
						FlyModule:Enable(player)
					end)

					Zone.playerExited:Connect(function()
						print("Player left:", spacePart.Name)
						FlyModule:Disable(player)
					end)
				else					
					warn("SpacePart NOT found for "..gardenName.."; repeating until found")
					
					repeat
						task.wait(0.1)
						spacePart = container:FindFirstChild("Space")
					until spacePart
					if spacePart then
						print("SpacePart found for "..gardenName)
					end
				end			
			end
		end
	else
		warn("PlayerGarden not found")
	end
end

local playerGarden = waitForPlayerGarden(player)
setupZones(playerGarden)
2 Likes

Thing is, you’re only calling the function for the local player (only the local player’s garden) and not for other players, use Players.PlayerAdded

i tried that and when i do, the whole thing just doesn’t work. I could be doing it wrong tho

Could you show the way you’re handling it with players.playeradded?

You’re going to have to redo everything on a server script and not a local script. That means you’ll have to rewrite some codes such as:

local player = game:GetService("Players").LocalPlayer

to

local PlayerName = script.Parent.Name -- Assuming this is in CharacterStarterScripts, else change it to your needs
local player = game:GetService("Players"):FindFirstChild(PlayerName)

Looks like the issue is that you’re only calling the function for the local player, so it’s not applying to others. Try using Players.PlayerAdded to handle all players joining the game. If that didn’t work, show how you’re implementing it—there might be something off in the way it’s set up.

here

local function onPlayerAdded(player)
	local playerGarden = waitForPlayerGarden(player)
	setupZones(playerGarden)
end

Players.PlayerAdded:Connect(onPlayerAdded)