Hey Developers,
I have this Script down below (it works fine). Basically the Script is in a Part and checks if the Part is touched if the Players is in a specific Group. It is like a Daily Group Reward Part so it Timer which goes constantly down until 0.
There is a TextLabel in a Billboard Gui which shows the Player if the Rewards are ready or not ready (second script down below). My question is that the TextLabels Text should not be READY or NOT READY if the Player joined or Touched the Part, is there a way to get the Time of the Player (in hours) and put it in the Text Labels Text? For example if the Player has 3 hours left before the Rewards the Text should be "Come back in " …theTime… " for the Daily Group Reward and so on.
Any help would be appreciated!
Here is the Script in the Part:
Part = script.Parent
DataStore = game:GetService("DataStoreService"):GetDataStore("RankRewards2")
Debounce = true
function PlayerAdded(Player)
local Time = DataStore:GetAsync(Player.UserId)
if not Time or (os.time() - Time) > 86400 then
game.ReplicatedStorage.Network.UpdateChests:FireClient(Player, "Update")
else
game.ReplicatedStorage.Network.UpdateChests:FireClient(Player, "Brake")
end
end
function Touched(Hit)
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Debounce == true then
if Player:IsInGroup(13278651) then
Debounce = false
if Player ~= nil then
local Time = DataStore:GetAsync(Player.UserId)
if not Time or (os.time() - Time) > 86400 then
DataStore:SetAsync(Player.UserId, os.time())
game.ReplicatedStorage.Network.UpdateChests:FireClient(Player, "Brake")
print("Player Redeemed!")
else
print("Not Ready Yet!")
end
end
Debounce = true
else
print("You are not in our group!")
end
end
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
Part.Touched:Connect(Touched)
and here is the second Script (local script):
game.ReplicatedStorage.Network.UpdateChests.OnClientEvent:Connect(function(Type)
if Type == "Brake" then
game.Workspace.Activations.GroupRewards.Show.Other.Text = "NOT READY!"
elseif Type == "Update" then
game.Workspace.Activations.GroupRewards.Show.Other.Text = "READY!"
end
end)
-- server script
-- get the time when the player last collected there reward
local playerTime = DataStore:GetAsync(player.UserId)
-- set this time in there numberValue
player.Time.Value = playerTime
-- localscript
-- how long the player must wait for there next reward
local waitTime = 86400
-- the number value inside the player
local timeValue = game.Players.LocalPlayer.Time
-- the textlabel we want to set
local textLabel = ...
-- this function changes seconds to readable text
local function SecondsToString(seconds)
local days = math.floor(seconds / 86400)
local hours = math.floor(seconds % 86400 / 3600)
local minutes = math.floor(seconds % 3600 / 60)
seconds = math.floor(seconds % 60)
return string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
end
-- loop once every second
while true do
-- how many seconds has past since they last claimed there reward
local deltaTime = os.time() - timeValue
-- if it was longer then 86400 say ready or print how long they have left
if deltaTime > waitTime then
textLabel.Text = "Ready"
else
textLabel.Text = SecondsToString(waitTime - deltaTime)
end
task.wait(1)
end
I tried that but the Output says Time is not a valid member of Player “Players.0xzMq”. Oh and should I add the ServerScript Lines or just the Local Script in the Local Script? Thank you for your fast reply!!!
you need to somehow tell the player the time in my demo code i used a NumberValue that is inside the player for the player to access if you dont put a NumberValue inside the player then it will error
the server script lines should execute when the player is added to the game
for each player
Ok so I make a Saving String or In Value when the Player Joins and add it into the Joined Player. Once the Player is in the Game or left the Game the Time goes constantly down until the Time reached the Wait Time. Once the Player joined the Game again the Time goes down the Time the Player was not in game or in game. When the Time reached 0 the Text should just say READY until the Player touched the Part, after he touched the Part the function should repeat.
-- get the time when the player last collected there reward
local playerTime = DataStore:GetAsync(player.UserId)
-- create a number value and put it into the player
local numberValue = Instance.new("NumberValue")
numberValue.Name = "Time"
numberValue.Value = playerTime
numberValue.Parent = player
Alright I know how to Save this Value and how to make it constantly go down, but how to add the function that the Time goes down the Time Period the Player was not in Game (for example the waiting Time was 18 hours left, the Player left and joined again 15 hours later => the waiting Time should be 3 Hours now)
Oh ok so i don’t have to add the function that the Time value goes down. But what makes it go down then? if it is the local Script, the Value would not save when the Player Rejoins or?
-- server script
local waitTime = 86400
game.Players.PlayerAdded(function(player)
-- get the time when the player last collected there reward
local playerTime = DataStore:GetAsync(player.UserId)
-- create a number value and put it into the player
local numberValue = Instance.new("NumberValue")
numberValue.Name = "Time"
numberValue.Value = playerTime
numberValue.Parent = player
end)
workspace.Claim.Touched:Connect(function(part)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player == nil then return end
-- how many seconds has past since they last claimed
local deltaTime = os.time() - player.Time.Value
-- if they have not waiting long enough exit the function
if deltaTime < waitTime then return end
-- update the time also make sure this time is saved in the data store so it is remembered when they enter the game again
player.Time.Value = os.time()
-- give the player there reward
end)
-- localscript
-- how long the player must wait for there next reward
local waitTime = 86400
-- the number value inside the player
local timeValue = game.Players.LocalPlayer.Time
-- the textlabel we want to set
local textLabel = ...
-- this function changes seconds to readable text
local function SecondsToString(seconds)
local days = math.floor(seconds / 86400)
local hours = math.floor(seconds % 86400 / 3600)
local minutes = math.floor(seconds % 3600 / 60)
seconds = math.floor(seconds % 60)
return string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
end
-- loop once every second
while true do
-- how many seconds has past since they last claimed there reward
local deltaTime = os.time() - timeValue
-- if it was longer then 86400 say ready or print how long they have left
if deltaTime > waitTime then
textLabel.Text = "Ready"
else
textLabel.Text = SecondsToString(waitTime - deltaTime)
end
task.wait(1)
end
os.time() tells you the current time in second so lets say
i claimed the reward at 1000
and the current os.time is 1100
local deltaTime = os.time() - timeValue
deltaTime would be 1100 - 1000 = 100
that means i have waited 100 seconds since the last claimed
now after some more time pasts
os.time is 1200
so
1200 - 1000 = 200
so that means i have waited 200 seconds and thats how the localscript is alwats updating the text label
and the reason we do it this way is so we don’t have to keep looping on the server side sending data over the internet to all the clients
Oh my god that seems to Work! The time goes down even if the Player is not in Game, The value shows the Time which is left and it says Ready if the Time reached 0 (I will add the function that if the Player is in the Group it only gives the Reward. I marked it as the Solution you are pretty close to 100 Solutions!!!
ok Ive tested it and I tried resetting the Time back to original but that didn’t work… Heres the Code:
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player == nil then return end
-- how many seconds has past since they last claimed
local deltaTime = os.time() - player.Time.Value
-- if they have not waiting long enough exit the function
if deltaTime < waitTime then return end
-- update the time also make sure this time is saved in the data store so it is remembered when they enter the game again
player.Time.Value = waitTime
print("Rewarded")
-- give the player there reward
end)