Hey there, I recently gutted out all of the scripts in my game and redid them to make them more efficient for me to update them in the future, and just more efficient for player performance in general.
Recently I’ve received reports, along with noticed myself that players are somehow getting EXP even when they aren’t in-game? I’m hoping that you guys can review this code and possibly find out what’s going wrong here. I’ve looked over it multiple times now, and just can’t find out what is going wrong here.
while wait(60*3) do
if cash then
if plr.Team ~= Teams["Citizens"] and plr.Team ~= Teams["Prisoners"] and plr.Team ~= Teams["Lobby"] then
cash.Value = cash.Value + 50
exp.Value = exp.Value + 6
elseif plr.Team == Teams["Citizens"] then
cash.Value = cash.Value + 25
elseif plr.Team == Teams["Prisoners"] then
cash.Value = cash.Value + 5
if exp.Value > 0 then
exp.Value = exp.Value - 5
end
end
wait(2)
local pt = plr.leaderstats["Prison Time"].Value
local ex = exp.Value
local csh = cash.Value
if cash.Value > 0 then
end
database:SetAsync(plr.userId, {
ex,
pt,
csh
})
print ("All data saved for "..plr.Name)
end
end
Players.PlayerAdded:Connect(createLeaderStats)--for new players after script connection runs
for _,player in pairs(Players:GetPlayers()) do -- for players who already joined
spawn(function()createLeaderStats(player)end)
end
while wait(180) do
for i,plr in pairs(game.Players:GetPlayers())do
if cash then
if plr.Team ~= Teams["Citizens"] and plr.Team ~= Teams["Prisoners"] and plr.Team ~= Teams["Lobby"] then
cash.Value = cash.Value + 50
exp.Value = exp.Value + 6
elseif plr.Team == Teams["Citizens"] then
cash.Value = cash.Value + 25
elseif plr.Team == Teams["Prisoners"] then
cash.Value = cash.Value + 5
if exp.Value > 0 then
exp.Value = exp.Value - 5
end
end
local pt = plr.leaderstats["Prison Time"].Value
local ex = exp.Value
local csh = cash.Value
database:SetAsync(plr.userId, {
ex,
pt,
csh
})
print ("All data saved for "..plr.Name)
end
end
Personally, I would handle everything inside the same script within a PlayerAdded event for ease. I’m not sure where your while loop was contained within but if it was contained within a CharacterAdded event then the while loop would run for each player each time his/her character reloaded/spawned.
local players = game:GetService("Players")
local teams = game:GetService("Teams")
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("DataStore")
players.PlayerAdded:Connect(function(player)
local key = player.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local pt = Instance.new("IntValue")
pt.Parent = leaderstats
pt.Name = "Prison Time"
pt.Value = 0
local cash = Instance.new("IntValue")
cash.Parent = leaderstats
cash.Name = "Cash"
cash.Value = 0
local exp = Instance.new("IntValue")
exp.Parent = leaderstats
exp.Name = "Exp"
exp.Value = 0
local res = ds:GetAsync(key)
if type(res) == "table" then
pt.Value = res[1]
cash.Value = res[2]
exp.Value = res[3]
end
task.spawn(function()
while task.wait(180) do
if player.Team == teams["Citizens"] then
cash.Value += 25
exp.Value += 5
elseif player.Team == teams["Prisoners"] then
cash.Value += 5
if exp.Value >= 5 then
exp.Value -= 5
end
elseif player.Team == teams["Lobby"] then
cash.Value += 5
exp.Value += 5
end
end
end)
player.CharacterAdded:Connect(function(character)
local timeVal = pt.Value
local cashVal = cash.Value
local expVal = exp.Value
local data = {timeVal, cashVal, expVal}
ds:SetAsync(key, data)
end)
end)
Also I’d recommend setting up unique checks for each possible team and then handle what stats should be awarded accordingly.