Help with script

#1

Script below:
– Daily Rewarder/Leaderstarts –

local ds = game:GetService(“DataStoreService”):GetDataStore(“UserData12”) – UserData can be everything like “PlayerData” etc. It’s up to you
------ Variables ------
local rewardTimeD1 = 24 – This must be in hours. 24 hours = 1 day. 0.001 = 3.6 seconds
local rewardBonusD1 = 20

local rewardTimeD2 = 48 – This must be in hours. 24 hours = 1 day. 0.001 = 3.6 seconds
local rewardBonusD2 = 70

local rewardTimeD3 = 72 – This must be in hours. 24 hours = 1 day. 0.001 = 3.6 seconds
local rewardBonusD3 = 150

local rewardTimeD4 = 96 – This must be in hours. 24 hours = 1 day. 0.001 = 3.6 seconds
local rewardBonusD4 = 300

local rewardTimeD5 = 120 – This must be in hours. 24 hours = 1 day. 0.001 = 3.6 seconds
local rewardBonusD5 = 400

local rewardTimeD6 = 144 – This must be in hours. 24 hours = 1 day. 0.001 = 3.6 seconds
local rewardBonusD6 = 600

local DailyRewarder = game.ServerStorage:WaitForChild(“DailyReward”)
----- Main Script -----
game.Players.PlayerAdded:connect(function(player)
local key = “user:”…player.userId – Unique user id of the player
local folder = Instance.new(“Folder”,player)
folder.Name = “leaderstats”
local cash = Instance.new(“IntValue”,folder)
cash.Name = “Cash” – Name of your currency
cash.Value = 1000 – Starting money of the player
local increment = Instance.new(“IntValue”,player)
increment.Name = “BonusIncrement”
increment.Value = 0

local save
pcall(function() -- What this does is if there is an error. It will not be called and the script would not break but instead continue to execute the script.
	save = ds:GetAsync(key)
end)
if save then
			print("No error in saving Daily Rewarder.")
	if (os.time()-save.rewardWait)/3600 >= rewardTimeD1 then
       save={cash=save.cash+(rewardBonusD1+save.increment);rewardWait=os.time();increment=save.increment+rewardBonusD1}
		ds:SetAsync(key,save) 
		DailyRewarder.Parent = game.Workspace
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(0,225,0) -- Changes Indicator to green
		wait(4)
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(255,0,0)-- Resets all Indicators
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(255,0,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Parent = game.ServerStorage
		else if (os.time()-save.rewardWait)/3600 >= rewardTimeD2 then
		save={cash=save.cash+(rewardBonusD2+save.increment);rewardWait=os.time();increment=save.increment+rewardBonusD2}
		ds:SetAsync(key,save)
					DailyRewarder.Parent = game.Workspace
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(0,225,0)-- Changes Indicator to green
		DailyRewarder.Frame.CashIndicatorD2.BackGroundColour3 = Color3.new(0,255,0)
		wait(4)
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(255,0,0)-- Resets all Indicators
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(255,0,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Parent = game.ServerStorage
			else if (os.time()-save.rewardWait)/3600 >= rewardTimeD3 then
		save={cash=save.cash+(rewardBonusD3+save.increment);rewardWait=os.time();increment=save.increment+rewardBonusD3}
		ds:SetAsync(key,save)
					DailyRewarder.Parent = game.Workspace
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(0,225,0)-- Changes Indicator to green
	    DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(0,225,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(0,255,0)
		wait(4)
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(255,0,0)-- Resets all Indicators
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(255,0,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Parent = game.ServerStorage
			else if (os.time()-save.rewardWait)/3600 >= rewardTimeD4 then
		save={cash=save.cash+(rewardBonusD4+save.increment);rewardWait=os.time();increment=save.increment+rewardBonusD4}
		ds:SetAsync(key,save)
		DailyRewarder.Parent = game.Workspace
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(0,225,0)-- Changes Indicator to green
        DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(0,225,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(0,255,0)
		DailyRewarder.Frame.CashIndicatorD4.BackGroundColour3 = Color3.new(0,255,0)
		wait(4)
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(255,0,0)-- Resets all Indicators
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(255,0,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Parent = game.ServerStorage
			else if (os.time()-save.rewardWait)/3600 >= rewardTimeD5 then
		save={cash=save.cash+(rewardBonusD5+save.increment);rewardWait=os.time();increment=save.increment+rewardBonusD5}
		ds:SetAsync(key,save)
					DailyRewarder.Parent = game.Workspace
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(0,225,0)-- Changes Indicator to green
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(0,225,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(0,255,0)
		DailyRewarder.Frame.CashIndicatorD4.BackGroundColour3 = Color3.new(0,255,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(0,255,0)
		wait(4)
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(255,0,0)-- Resets all Indicators
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(255,0,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Parent = game.ServerStorage
			else if (os.time()-save.rewardWait)/3600 >= rewardTimeD6 then
		save={cash=save.cash+(rewardBonusD6+save.increment);rewardWait=os.time();increment=save.increment+rewardBonusD6}
		ds:SetAsync(key,save)
		DailyRewarder.Parent = game.Workspace
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(0,225,0)-- Changes Indicator to green
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(0,225,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(0,255,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(0,225,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(0,255,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(0,255,0)
		wait(4)
		DailyRewarder.Frame.CashIndicatorD1.BackgroundColour3 = Color3.new(255,0,0)-- Resets all Indicators
		DailyRewarder.Frame.CashIndicatorD2.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD3.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD4.BackgroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Frame.CashIndicatorD5.BackGroundColour3 = Color3.new(255,0,0)
	    DailyRewarder.Frame.CashIndicatorD6.BackGroundColour3 = Color3.new(255,0,0)
		DailyRewarder.Parent = game.ServerStorage
                        end
                  end
	        end
       end
 end

end
cash.Value = save.cash
increment.Value = save.increment
else
ds:SetAsync(key,{cash=1000;increment=0;rewardWait = os.time()}) – The value of the cash here must be the same as the starting cash on top. For example if you have 20 starting cash, the value here must also be 20.
end
end)
– Scripted by: ZylanchewYT

0 Likes

#2

Can you please format the whole script?

What do you need help with? Just the script pasted into a post isn’t enough information.

0 Likes

#3

Sorry… I do not need this thread anymore as my friend helped me.

0 Likes

#4

Can you please post your solution so others with the same problem can find a solution quicker?

1 Like

#5

Welcome to the DevForum. Please excuse my merciless reply. I just want to give you a few pointers on both your thread and your code. Make sure you familiarise yourself with the forums, the rules and various informational threads so your experience here can be at its peak.

  • Please read the sticky of this category before posting.
  • Post does not have proper formatting: please fix this by enclosing all your code within a code block (three back ticks at the beginning ``` and at the end).
  • Post does not include enough information to determine what your issue is.
  • If you’re going to outsource help, please finish requesting help from those sources before posting here - your question may get answered and thus not need a thread, like this.
  • If you found a solution to the problem, please explain what that solution is so others with the same problem may be able to read the solution.

To address the code itself, I’m no dictator on how you should write you code since it’s up to preference, but thee’s generally some things you should avoid doing: stacking the reward constants the way you did is one thing. You can easily relegate this to a table.

local Rewards = {
-- format [hours] = data
}

You should not create instances and utilise the parent argument. In the long run you should get used to creating the instance, setting the properties and then the parent, for performance purposes. Though this is sort of premature optimisation, I personally would change over. It’s consistent with my coding conventions anyway.

There’s no failure redundancy in this code. If a player’s data doesn’t retrieve, the server doesn’t block saving requests. People who experience data loading issues will be given data and the code will save new information to the data store, which will result in permanent data loss that has to be manually rolled back. This is terrible for UX and your game will see significant drops in stats all-around.

I can’t quite catch what you’re trying to do with those nested if statements. They’re repetitive code blocks all over. You can crunch that all down to running once. Everything inside that singular code block can be determined by one value (i.e. number of days lights up depending on what time since they last entered was). This part of the code was so messy that I couldn’t understand much of what was going on, which brings me to my next point addressing your manner of handling daily rewards.

Regardless of what you’re doing, daily rewards are supposed to be daily. The way you’re handling it is poor and it’s trying to calculate the hours equivalent to a week. The way you should be handling it is per day. Every day someone logs into the game, the reward function needs to go to work. Instead of trying to calculate rewards over the whole week, check how many consecutive days a player has gone on. If they’ve been on for more than 7 days consecutively (full week) or their streak is broken, set it back to 1 and give them 1x the reward. Every other day from 2-7 should give nx the reward based on the consecutive day reached.

Also, pet peeve: coding notation (i.e. author name) should be at the top, not the bottom.

All in all, in the long run I think it would be better that you appropriately refactor your code so you can save yourself a major headache and give players the best UX possible while playing your game. Data is very important to many games that are stat-based and not having the adequate code to support such data may be detrimental for you. If you seek further help, feel free to begin a new thread (with the given feedback - don’t ignore it!) or post for extra help. :slight_smile:

3 Likes

#6

If you have found a solution, I suggest you reply the solution to your problem then mark that as the solution. Since there is no reply marked as a solution, people may come into this thread looking to provide a solution, which of course is useless if you already have one.

0 Likes

unlisted #7
0 Likes

#8

This post doesn’t meet the criteria for Scripting Support.

Please follow the format in the future or your posts will be locked.

Criteria for Scripting Support can be found here. Every category has a pinned “About …” post that explains what you should and shouldn’t post in the category, and how to post it:

0 Likes

closed #9
0 Likes