Scripting Help Needed

I’ve got a daily reward system and separately I’ve got a script that links it to the UI. Now I don’t know how to connect the script and when I do it says remote function is an unknown global and so is player. Currently, p is the unknown global.

local datastoreService=game:getService("DataStoreService")
local datastore=datastoreService:GetDataStore("PlayerData")

--Settings
local DailyRewardWait=60/3600 --in hour
local reward=20

game.Players.PlayerAdded:connect(function(p)
    --Create leaderstats and values
    local leaderstats=Instance.new("Folder")
    leaderstats.Name="leaderstats"
    leaderstats.Parent=p
    local PandaCoins=Instance.new("IntValue")
    PandaCoins.Name="PandaCoins"
    PandaCoins.Parent=leaderstats
    
    --Get data and get data checks
    local userdata
    pcall(function()
        userdata=datastore:GetAsync(p.UserId)
    end)
    if userdata then
        if (os.time()-userdata.rewardTime)/3600>=DailyRewardWait then
            userdata={PandaCoins=userdata.PandaCoins+reward; rewardTime=os.time();}
            datastore:SetAsync(p.UserId,userdata)
        end
        PandaCoins.Value=userdata.PandaCoins
    else    
    end
        --[[
            Table format:
            PandaCoins=0;
            rewardTime=24029403294
        --]]    
        datastore:SetAsync(p.UserId,{
            PandaCoins=0;
            rewardTime=os.time();
        })
        end    
``` I got that script and I want to add this one to it: ```
--Server
remoteFunction:InvokePlayer(player, {Reward = SOMETHING})

--Reward the player, this piece of code will only run after the client responds

--Client
function remoteFunction.OnClientInvoke(data)
    if data.Reward then
        local answer

        --Display the ui

        button.MouseButton1Down:Connect(function()
            answer = true
        end)

        repeat wait() until answer

        return true
    end
end ```
1 Like

Could you send a download with the GUI? It would be helpful so we can actually see whats up.

RF’s are a bit different. You link them like this.

RemoteFunction.OnClientInvoke = function(args)
end

When the client invokes the server through that RF, the function should be called.

Where do I put that? Also I was told to create a remote function in relocated storage. Do I put anything under it? The button is not clickable even the though I put the display UI script under it.

Change it to this

remoteFunction.OnClientInvoke = function(data)
    if data.Reward then
        local answer

        --Display the ui

        button.MouseButton1Down:Connect(function()
            answer = true
        end)

        repeat wait() until answer

        return true
    end
end

Do I put that display UI in a local script under the image button. Then I need to create one for the GUI to hide it once that button is clicked?

Just replace this with the script I sent you image

I suggest you create a LocalScript that sends/receive requests from RemoteFunctions. Then, create a Script that accurately displays that data to the player by using InvokeClient.

This would be easier if you didn’t give the client a choice. There are many games which uses a daily reward system and points/coins are automatically given without the player’s decision.

This post is an example of what it should look like.

I’ve modified the code from that post and adjusted it so that it works with your code. Make sure there is a RemoteFunction in ReplicatedStorage named DailyReward. Also, use this remote so that the client is notified that they have received a reward. @PandaPb use this code and let me know if it works.

--Credits to Xeptix for the starting code.
local DataStoreService = game:GetService('DataStoreService')
local RewardDataStore = DataStoreService:GetDataStore('RewardsDataStore', '001')
local datastore = DataStoreService:GetDataStore("PlayerData")
local reward = 20

local DailyReward = game:GetService("ReplicatedStorage"):WaitForChild("remoteFunction")

game.Players.PlayerAdded:connect(function(Player)
	local RewardData = RewardDataStore:GetAsync(tostring(Player.UserId))
	local userdata = datastore:GetAsync(tostring(Player.UserId))
	
    local leaderstats=Instance.new("Folder")
    leaderstats.Name="leaderstats"
    leaderstats.Parent=Player

    local Cash=Instance.new("IntValue")
    Cash.Name="Cash"
    Cash.Parent=leaderstats
    
	
	if typeof(RewardData) == "table" then
		local LastLogin = RewardData[1]
		local DaysInRow = RewardData[2]
		local Seconds = os.time() - LastLogin
		local Minutes = Seconds / 60
		local Hours = Minutes / 60
	
		if Hours >= 12 and Hours <= 36 then -- they logged in 12 to 36 hours since their last (change as you desire)
			RewardDataStore:SetAsync(tostring(Player.UserId), {os.time(), DaysInRow + 1})
			userdata.Cash = userdata.Cash + reward
			DailyReward:InvokeClient(Player,reward)
		end
	else
		RewardDataStore:SetAsync(tostring(Player.UserId), {os.time(), 0})
		userdata.Cash = userdata.Cash + reward
		DailyReward:InvokeClient(Player,reward)
	end
end)
1 Like

I’m new to scripting so does that mean I use the script above in remote function and create a separate one to send and receive ?

1 Like

Thanks, I’ll make sure to try it out. How do I use the the remote so that the client is noticed when they have received a reward?

I’ve added this script under my daily reward system, added a remote function and added a local script that says :
local remoteFunction = game:GetService("ReplicatedStorage"):WaitForChild("remoteFunction")

I’ve also added this to the daily reward script. I’ve added this to the text button

button.MouseButton1Down:Connect(function()
answer = true
end)

    repeat wait() until answer

    return true
end

end

the button doesn’t seem to work so I need to add a hide option.

You don’t need a button. Just have a frame from the user interface on the client side that is visible for an x amount of seconds when InvokeClient is called. Could you screenshot your explorer so I can see the frames?

I had an image label and an image button because I made a custom UI

Could you paste your entire Script here? This includes the changes you made after I modified the code. I want to read over it so I can possibly fix any bugs and construct the LocalScript for you.

local datastore =datastoreService:GetDataStore("PlayerData")

--Settings
local dailyRewardWait=24 -- in hours
local reward=20

game.Players.PlayerAdded:connect(function(p)
	--Create leaderstats and values
	local leaderstats=Instance.new("Folder")
	leaderstats.Parent=p
	local Cash=Instance.new("IntValue")
	Cash.Name="Cash"
	Cash.Parent=leaderstats
	
	--Get data and do data checks
		local userdata
		pcall(function()
			userdata=datastore:GetAsync(p.UserId)
		end)
		if userdata then
			if (os.time()-userdata.rewardTime)/3600>=dailyRewardWait then
				userdata={Cash=userdata.Cash+reward; rewardTime=os.time()}
				datastore:SetAstnc(p.UserId, userdata)
			end
			Cash.Value=userdata.Cash
		else
			--[[
				Table Format:
				Cash=0;
				rewardTime=24029403294
				
			]]
			datastore:SetAsync(p.UserId,{
				Cash=0;
				rewardTime=os.time();
			})
			
		end
end)``` 

I have changed the IntValue to cash. This is currently working.

I updated the code above with the new values but could you use the code I provided above? Tell me if that works and I’ll work on making the LocalScript work.

Should I replace my original code with that one or add on to it? What is the one you provided exactly for?

Replace your original code with the one I gave you. I included the rewards system and the cash system. The only thing that’s probably not working is the UI. I still have to create the LocalScript for it, but I need to know if the code I gave you works in the game.

I’m not sure how to check if it’s working but no errors or warning pop up for me.