Sometimes my scripts run, sometimes they do nothing at all

Hello.
I’ve recently added two new scripts to my game. One in ServerScriptService (script) and one in a Gui stored under StarterGui (LocalScript).

Sometimes these scripts run perfectly, whereas other times they don’t run at all. They handle a daily reward system but it doesn’t actually show up when I need it to. I’ve had this issue with other scripts and I added a wait() function to it which seemed to fix the problem, but that isn’t the case with these scripts.

Is there anything else I can do to fix it?
Thanks.

1 Like

We can’t tell what’s going on if you don’t post the scripts.
Please put 3 backticks before and after the script (```) so when you copy/paste it formats properly here.

Script in ServerScriptService

local DataStore = game:GetService("DataStoreService"):GetDataStore("DailyRewards")

local hourWait = 0.0000000000000001

local possibleRewards = {1000,2000,1000,1000,1000,500,500,500,500,500,500,500,500,500,500,1000,1000,1000,2000,2000,10000}


game.Players.PlayerAdded:Connect(function(player)
	
	local cash = player.leaderstats.Cash.Value
	
	local timeNow = os.time()
	
	local data
	
	pcall(function()
		data = DataStore:GetAsync(player.UserId.."-dailyReward") -- 14930679-dailyReward
		print("Getting Data")
	end)
	
	if data ~= nil then
		-- Returning player to the game
		
		local timeSinceLastClaim = timeNow - data -- Number in sec since the last reward was claimed
		
		print("Time since last claim: "..timeSinceLastClaim)
		
		if (timeSinceLastClaim / 3600) >= hourWait then
			-- They are eligible for a reward
			local reward = possibleRewards[math.random(1,#possibleRewards)]
			wait(3)
			game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
			local connection
			connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
				if triggeringPlayer == player then
					print("Reward Claimed")
					cash = cash + reward
					DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
					connection:Disconnect()
				end
			end)
		else
			print("Player is uneligible right now")
		end
		
		
	else
		-- New Player
		print("New Player")
		wait(3)
		local reward = possibleRewards[math.random(1,#possibleRewards)]
		game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
		local connection
		connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
			if triggeringPlayer == player then
				print("Reward Claimed")
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + reward
				DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
				connection:Disconnect()
			end
		end)
	end
	
end)

LocalScript in StarterGui

game.ReplicatedStorage.ShowDailyReward.OnClientEvent:Connect(function(hoursToNextReward, rewardAmount)
	wait(3)
	script.Parent.Enabled = true
	script.Parent.MainFrame.ComeBackText.Text = "Thank you for playing! Come back in "..hoursToNextReward.."for your next reward*."
	
	script.Parent.MainFrame.Claim.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.ClaimReward:FireServer()
		script.Parent.RewardNotice.Text = "You have received "..rewardAmount.."!"
		script.Parent.RewardNotice:TweenPosition(UDim2.new(0.5, 0,0.1, 0), "InOut", "Quad", 0.5, true)
		wait(2)
		script.Parent.MainFrame.Visible = false
		wait(1.5)
		script.Parent.RewardNotice:TweenPosition(UDim2.new(0.5, 0,-0.1, 0), "InOut", "Quad", 0.5, true)
		wait(1.5)
		script.Parent.Enabled = false
		script.Parent.MainFrame.Visible = true
	end)
end)

My theory
I believe the issue is how instances are loaded.

Use :WaitForChild("<INSTANCE_NAME>")
Read more at: Instance | Documentation - Roblox Creator Hub

If that doesn’t solve the issue
Then, I believe it is some assignment or condition in the script.

Can you provide any error messages? When you notice it acting up, what does the dev console say? Did you try to use print() or warn() with the assignments/variables to understand more about the program?

How to improve your programming?

Note: these are tips. You are NOT required to follow them! :smiley:

  1. Never use wait()
    use task.wait(), it is more efficient
    task | Documentation - Roblox Creator Hub
-- Use
task.wait(1)

-- Instead of
wait(1)
  1. How to get Services (:GetServices())
--Use
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--Instead of
game.ReplicatedStorage

-- Example
ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
-- code
end)

Why? Limit the number of times you request the same object. Define it once then use the object already defined. More efficient

More information @ Services | Documentation - Roblox Creator Hub

2 Likes

My theory is the playeradded event isn’t firing in-time. Maybe a player joins before that event is setup? You can fix this by looping all players after setting it up

example:

local Players = game:GetService("Players")

local function PlayerAdded(plr)
  -- your playeradded function
end

Players.PlayerAdded:Connect(PlayerAdded)

for _,v in pairs(Players:GetPlayers()) do -- just incase player joined before the event was setup.
  PlayerAdded(v)
end

I’ll give these a try. Thank you!

Unfortunately, none of this worked.
There aren’t any error messages being printed at all. In the code, there are print messages yet when the code doesn’t work, these messages aren’t printed. When it does work, these messages are printed.

Did this help?

I agree with their theory.

Yeah I also gave that a try. It worked the first two times but then the third time it stopped working again.

1 Like