Okay so I am trying to increase “TİmePlayed” when player is playing the game but. it increases even though player is not in game
(Don’t mind the PlayerAdded part too much just shared so you can have a idea about what is “TimePlayed” or “PlayerJoinOsTime”)
-- players.PlayerAdded:Connect(function(player)
local succes,Data = pcall(function()
return GameDataName:GetAsync(player.UserId)
end)
if succes then
if Data then
-- if player is not new
DailyRewardsChecker[player.UserId] = {TimePlayed = Data["TimePlayed"], PlayerJoinOsTime= os.time()}
print("NotNewPlayer")
for k, d in pairs(Data) do
if k == "RewardsTable1" then
DailyRewardsChecker[player.UserId] = d
print("Welp")
else
if k == "RewardsTable2" then
ClaimedRewards[player.UserId] = d
end
end
end
else
ClaimedRewards[player.UserId] = {}
warn("NewPlayer")
DailyRewardsChecker[player.UserId] = {TimePlayed = 0, PlayerJoinOsTime= os.time()}
end
--Something Wrong with DataStore script or servers are down.
end
local function DailytimeChecker(player,menon)
--PlayerJoinOsTime is the time when players joins the game for the first time
menon["TimePlayed"] = os.time() - menon["PlayerJoinOsTime"]
return {Time=menon["TimePlayed"]}
end
end)
Remotes.RewardCheck.OnServerEvent:Connect(function(player,2ndArgument)
warn("I got the".. ButtonName)
local time =DailytimeChecker(player,DailyRewardsChecker[player.UserId])
end)
You are calculating the time based on the difference between the current time (os.time() ) and the time when the player first joined the game (menon["PlayerJoinOsTime"] ). This means that if the player leaves the game and comes back later, their TimePlayed will still increase even though they were not actively playing the game during that time.
Try using Player.CharacterAdded and Player.CharacterRemoving events to detect when the player joins and leaves
You can use a Boolean value that changes when a player leaves and join and if it’s true then time will be added to the player.
-- Event to handle when a player's character is added
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
DailyRewardsChecker[player.UserId]["IsActive"] = true
end)
end)
-- Event to handle when a player's character is removed
game.Players.PlayerAdded:Connect(function(player)
player.CharacterRemoving:Connect(function(character)
DailyRewardsChecker[player.UserId]["IsActive"] = false
end)
end)
well. I didn’t tested but it probably wouldn’t work. The problem is with os.time- playerJoinOsTime
so, os.time will still increase even if IsActive is equals to false or true
let’say os.time = 1 and playerjoinOstime 1
1-1
it is 0
but if player leaves and comes after 20 seconds
it will 20 -1 so. yeah it will not help
and second thing is I would prefer to not use Character.added because it wouldn’t work if player’s character loads faster than the script
What I’d do is just save the TimePlayed as a separate variable could be SavedTime, on Player.CharacterRemoving and then when Player.CharacterAdded you reset their TimePlayed back to 0 and then add the SavedTime to it.
Can you explain it more?
I used Os.time to add “1” each second and to check timeplayed number. how can I add 1 each second while using a number value
do you mean something like
while PlayerIsInGame = =true do
Playing time += 1
task.wait(1)
end
but if I remember correctly since while will work until player leaves the game, the codes after the while statement won’t work
task.defer(function()
while task.wait(1) and PlayerIsinGame do
PlayingTime += 1
end
end)
If you want to get more advanced and be really precise (for whatever reason), you could use the given delta parameter of RunService.Heartbeat, although this would expand the integers into floats
local RunService = game:GetService("RunService")
local HeartbeatEvent = RunService.Heartbeat:Connect(function(delta) -- probably save heartbeatevent in a dictionary that you can access when the player leaves
PlayingTime += delta
end
game.Players.PlayerRemoving:Connect(function(plr)
HeartbeatEvent:Disconnect() -- would have to have an event for each player that you can disconnect individually when you no longer want to track the data (see above note)
end
well I was saying using while loop wouldn’t work, well I mean it wouldn’t work the way I won’t, of course it would increase the playtiem by 1 but… you understand it
is it really a must to save HeartbeatEvent?
I am asking this because I have never used RunService.Heartbeat before
but just saving playingTime should be enough
edit: well, I writed this reply like 2 hours ago. but WELP I forgot to post it