Daily Reward only gives to one person per server

I think you need to put the players in a table. This will create a table where all the players are in a server, making it valid to everyone. Hang on a second while I get it up.

Try this:
local plrs = {}

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player) -- Add each player into plrs table
	end
end

for i, player in pairs(plrs) do
if player then
character = player.Character

		if character then

function onTouch(hit)
if hit.Parent:findFirstChild(“Humanoid”) ~= nil and debounce == false then
if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
ThisPlayer.leaderstats:findFirstChild(currency).Value = >ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
script.Parent.Transparency = 1
script.Parent.CanCollide = false
debounce = true
script.Sound:Play()
wait(86400) – Respawn time for the cash giver
script.Parent.Transparency = 0
script.Parent.CanCollide = true
debounce = false
end
end
end

script.Parent.Touched:connect(onTouch)

Is this a Script or a LocalScript? If it is a Script, you would have to switch it with a LocalScript and move the LocalScript somewhere else (I would assume it’s a Script in a part because script.Parent seems to refer to a part). As for a LocalScript, you would have to check if the player that touched it is the LocalPlayer, then give the player their daily reward.

local part = game.Workspace.PartName
local localPlayer = game.Players.LocalPlayer

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if (player and player == localPlayer) then --Check if it's a player, and if it's the LocalPlayer
        --LocalPlayer touched
    end
end)
2 Likes

This is quite poorly made. Waiting for 86400 seconds wouldn’t do anything, because a wait server-side will stop when the server becomes empty (or if it’s on the client, when the player leaves). You need to save the next time to the player’s data by doing something like this:

PlayerData.DailyReward = os.time() + 86400

:connect() is also deprecated. Use :Connect() instead.

I believe this would be a better solution (fixed some errors and removed unnecessary parts). Of course, you’ll have to change it to adapt to your game.

currency = "Coins"	
amnt = 20					
debounce = false		

	function onTouch(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			Player.leaderstats:FindFirstChild(currency).Value = Player.leaderstats:FindFirstChild(currency).Value + amnt
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			debounce = true
			script.Sound:Play()
			Player.DailyReward = os.time() + 86400 -- wait until next day
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true
			debounce = false
			end
		end
	end


script.Parent.Touched:Connect(onTouch)
1 Like

I agree. Waits only apply to the player in the server, so if they leave the server, it will restart.

That isn’t always the case. If you have it on the client, then yes, the wait will restart when that client leaves. But if you have the wait on the server, then it’ll continue until the server closes.

2 Likes

Thank you, you corrected me. I meant to say something on the lines of that, just didn’t put it quite right. Hey, you know your stuff!

1 Like

I used the script and it didnt work
https://www.loom.com/share/4336befbd04241f18236d62419f372a8

As I said:

As the line that I added:
Player.DailyReward = os.time() + 86400 -- wait until next day
probably doesn’t correspond to your data saving. You’ll need to add a DailyReward value to your data handling.

Did you get any errors in your output?

AlvinBlox made a beautiful tutorial and explained this really well. Check it out:

1 Like

I’ve seen that one before, but I wanted mine to be unique. If I cant find the solution to the problem I have rn I might just use that instead. Thx tho :+1:

1 Like

Wait, i think you already made a post about this, please don’t make duplicate posts, if my solution didn’t work, you was able to tell me that my script that i gave you didn’t work.

I made the post again because no one was replying to them, and I feared that I might bother you/sound stupid if you have already made a reply and I still have a problem with the code. Sorry about that.

1 Like

So basically, this script that i gave you:

local Wspace = game:GetService("Workspace")
local RepStorage = game:GetService("ReplicatedStorage")
local PartToTouch = Wspace:FindFirstChild("DailyRewardBlock")

function onTouch(hit)
    if hit:FindFirstChild("Humanoid") then
        local DailyEvent = RepStorage:FindFirstChild("DailyEvent") --You must add a remote event in replicated storage!
        DailyEvent:FireServer() --Look at this line
    end
end

PartToTouch.Touched:Connect(onTouch)

The “FireServer()” event is not written correctly, change it to FireClient().

Also, no one will say you sound stupid, this is a forum to help. Any reply like that should be reported.

And the PartToTouch local, change it to the block that you touch to get the reward.

1 Like

so this is all I need to do. I feel like i need to add more code or something?

Nothing else i guess, test the script and tell me if there’s any error.

OK so i tried everything that you have said and also what other people have suggested as well and yet I cant seem to figure it. So is there like a .rblx file for me to look at and try and figure it out from there?

There’s any error in the output? 30chars

Also, in this script that i gave:

local RepStorage = game:GetService("ReplicatedStorage")
local DailyEvent = RepStorage:FindFirstCshild("DailyEvent")

local DailyRewardValue = 10

DailyEvent.OnServerEvent:Connect(function(player)
    local Leaderstats = player:FindFirstChild("Leaderstats")
    if (Leaderstats ~= nil) then
        local Debounce = Leaderstats:FindFirstChild("Debounce") --Add a Debounce BoolValue
        local DebounceTime = Leaderstats:FindFirstChild("DebounceTime") --Add an IntValue
        local Coins = Leaderstats:FindFirstChild("Coins")
        if (Coins ~= nil) and (Debounce ~= nil) and (DebouceTime ~= nil) then
            if not Debounce then
                Debounce.Value = true
                Coins.Value = Coins.Value + DailyRewardValue
                DebounceTime.Value = 86400
            end
        end
    end
end)

And this:

game.Players.PlayerAdded:connect(function(player)
    local Leaderstats = player:FindFirstChild("Leaderstats")
    if Leaderstats ~= nil then
        local Debounce = Leaderstats:FindFirstChild("Debounce")
        local DebounceTime = Leaderstats:FindFirstChild("DebounceTime") -- Add an IntValue in Leaderstats
        if (Debounce ~= nil) and (DebounceTime) ~= nil then

            while true do
                DebounceTime.Value = DebounceTime.Value - 1
                wait(1)
            end

            DebounceTime.Changed:Connect(function()
                if DebounceTime.Value <= 0 then
                    Debounce.Value = false
                end
            end)
        end
    end
end)

Change everything called “Leaderstats” to “leaderstats”

no I just feel like im missing a lot of stuff. What I mean is im used to seeing a lot of code as I work. (Somtimes I use one line code for simple tasks) And what u gave me seems to be really short for something that is going to be doing a lot.
Thats why im asking for a .rblx file so I can see what u made and campare/fix with mine.

The problem is that when someone gets the coin, it waits 86400 until the next person is able to get the coin. You should put this code in a local script, and fire it with a remote event whenever someone touches the coin. If you don’t know how clients and servers work, then Idk how to explain it.

Sorry, but i already gave you too much information and scripts to you. I think you should continue for yourself.

1 Like