I am creating a power simulator, I created the training area, when I enter I start to gain status, when I leave it I stop gaining status, it works well in roblox studio, but when I published it in roblox and joined my friend it doesn’t work , it is reported that there is no player in the training area
iam alone work, but on server with more than 1 player
See, not working, this print was supposed to be printed if player isnt on the area
Part code :
--Variables
local Part = workspace.Endurance0
local players = game:GetService("Players")
local trainingM = require(game.ServerStorage.TrainingFiles.TrainingModule)
local Zone = require(game.ServerStorage.TrainingFiles.ZoneModule)
local debounce = false
local waitTime = 1
local newZone = Zone.new(workspace.Endurance0)
while true do
wait(waitTime)
local playersInZone = newZone:getPlayers()
local playersInZoneDictionary = {}
for _, plr in pairs(playersInZone) do
playersInZoneDictionary[plr] = true
end
for _, plr in pairs(players:GetChildren()) do
if playersInZoneDictionary[plr] then
print("here"..plr.Name)
if not trainingM.GetTraining() then
else
if trainingM.GetTraining()[plr.UserId]["Training"] == false then
print("IAM HERE")
trainingM.SetTraining(plr,true)
trainingM.Training(plr,50,Part,"Endurance")
print("IAM HERE 2")
end
end
else
if not trainingM.GetTraining() then
else
if trainingM.GetTraining()[plr.UserId]["Training"] == true then
print("i cant be here")
trainingM.SetTraining(plr,false)
trainingM.StopTraining(plr)
end
end
end
end
end
TrainingModule code :
local training = {}
local trainingValues = {
["Training"] = false
}
local StartTraining = game.Workspace:WaitForChild("StartTraining")
local StopTraining = game.Workspace:WaitForChild("StopTraining")
local module = {}
function module.New(p)
training[p.UserId] = trainingValues
end
function module.SetTraining(player,value)
if value == nil then
training[player.UserId] = nil
else
training[player.UserId]["Training"] = value
end
end
function module.GetTraining()
return training
end
function module.Training(player,amount,Part,Stat)
StartTraining:Fire(player,amount,Part,Stat)
end
function module.StopTraining(player)
StopTraining:Fire(player)
end
return module
ServerScriptService code :
-- services
local Players = game:GetService("Players")
local Version = 1
local DataService = game:GetService("DataStoreService")
local PlayerData = DataService:GetDataStore("SimPowerStore_"..Version)
local MaxAttempts = 3
local StartTraining = game.Workspace:WaitForChild("StartTraining")
local StopTraining = game.Workspace:WaitForChild("StopTraining")
local TrainModule = require(game.ServerStorage.TrainingFiles.TrainingModule)
local Zone = require(game.ServerStorage.TrainingFiles.ZoneModule)
-- variables
local ServerData = {}
--training events--
local AutoTraining = {}
local AutoTrainingValues = {
["Training"] = false
}
local function increaseStats(player,Amount,Stat)
while true do
wait(1)
if AutoTraining[player.UserId]["Training"] == false then return end
ServerData[player.UserId]["Stats"][""..Stat]["Value"] = ServerData[player.UserId]["Stats"][""..Stat]["Value"] + 50
print(ServerData[player.UserId]["Stats"][""..Stat]["Value"])
end
end
local function checkIsTrainingArea(player,Amount,Part,Stat)
print("checkingg")
print(Amount)
AutoTraining[player.UserId]["Training"] = true
increaseStats(player,Amount,Stat)
end
local function stopTrainingF(player)
AutoTraining[player.UserId]["Training"] = false
end
StartTraining.Event:Connect(checkIsTrainingArea)
StopTraining.Event:Connect(stopTrainingF)
--end training events--
local function DataRetry(DataFunction,...)
local Tries = 0
local Data = nil
local Success,Message = false,nil
local Args = {...}
while Tries < MaxAttempts and not Success do
Tries = Tries + 1
Success,Message = pcall(function() Data = DataFunction(unpack(Args)) end)
if not Success then wait(1) warn("DataRetry Failed:",Message) end
end
if not Success then
warn("Data not found")
else
print("Data Found")
end
return Data
end
local function newData()
return {
["Stats"] = {
["Strength"] = {
["Value"] = 0;
["Suffix"] = "";
["Multiplier"] = 0;
["Boost"] = 0;
};
["Endurance"] = {
["Value"] = 0;
["Suffix"] = "";
["Multiplier"] = 0;
["Boost"] = 0;
};
["Mind"] = {
["Value"] = 0;
["Suffix"] = "";
["Multiplier"] = 0;
["Boost"] = 0;
};
["Jump"] = {
["Value"] = 0;
["Suffix"] = "";
["Multiplier"] = 0;
["Boost"] = 0;
};
["Speed"] = {
["Value"] = 0;
["Suffix"] = "";
["Multiplier"] = 0;
["Boost"] = 0;
}
};
["Leard"] = {
["Reputation"] = 0;
["Rank"] = "Innocent";
};
["Missions"] = {
};
["Skills"] = {
};
}
end
local function Get(p)
local Data = PlayerData:GetAsync("PlayerUserId_"..p.UserId)
return Data
end
local function loadData(p)
local data
local Success,Error = pcall(function()
data = DataRetry(Get,p)
end)
if Success then
if not data then
data = newData()
print("Generating new data")
end
else
p:Kick("Data not found pls Rejoin")
end
ServerData[p.UserId] = data
end
local function teste(p)
ServerData[p.UserId]["Leard"]["Reputation"] = 30
print(ServerData[p.UserId]["Leard"]["Reputation"])
end
local function saveData(p,SaveType)
if ServerData[p.UserId] then
local Success, Error = pcall(function()
PlayerData:UpdateAsync("PlayerUserId_"..p.UserId, function(oldData)
return ServerData[p.UserId]
end)
end)
if Success then
warn(p.Name.." Data saved ")
else
warn("Data wasn't saved for "..p.Name.." error "..Error)
end
if SaveType == "AutoSave" then
else
ServerData[p.UserId] = nil
end
end
end
local function loadLeardStats(p)
local folder = Instance.new("Folder")
local rank = Instance.new("NumberValue")
rank.Name = "Rank"
rank.Value = ServerData[p.UserId]["Leard"]["Rank"]
rank.Parent = folder
local reputation = Instance.new("NumberValue")
reputation.Name = "Reputation"
reputation.Value = ServerData[p.UserId]["Leard"]["Reputation"]
reputation.Parent = folder
folder.Name = "leaderstats"
folder.Parent = p
end
Players.PlayerAdded:connect(function(p)
TrainModule.New(p)
loadData(p)
loadLeardStats(p)
teste(p)
AutoTraining[p.UserId] = AutoTrainingValues
end)
game:BindToClose(function()
print("Closing")
for _,p in pairs(Players:GetChildren()) do
stopTrainingF(p)
DataRetry(saveData,p,"LeaveSave")
TrainModule.SetTraining(p,nil)
AutoTraining[p.UserId] = nil
end
end)
Players.PlayerRemoving:connect(function(p)
stopTrainingF(p)
DataRetry(saveData,p,"LeaveSave")
TrainModule.SetTraining(p,nil)
AutoTraining[p.UserId] = nil
end)
while true do
wait(180)
for _,p in pairs(Players:GetChildren()) do
DataRetry(saveData,p,"AutoSave")
end
end
local function x(Player)
print("Connected")
end
The ZoneModule (isnt mine, is from a player that make a post on this forum) here is the post Zone+ v1 (deprecated) | Retrieving players within an area/zone