My friend made a daily coin giver and for some reason it only works for one person per sever
EDIT: I’m new to scripting lua so much questions may sound dumb
currency = "Coins"
amnt = 20
debounce = false
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)
I mean, consider the fact that this function is debounced for 86400 seconds (24 hour yield). It’s not that it only works for one person, the delay is absurdly large. You need to find another way to do this.
Consider this: handle the coin’s appearance locally. The server and the client will work together here. The server will determine if the player is eligible for reward or not (has not claimed a reward in the last 24h) and tell the client. The client will then accordingly make the coin visible or not for that game session alone and then that’s the end of that.
If you don’t care about players being able to hop servers to get coins you only need to store the player’s UserId and the tick() they last used the coin giver.
currency = "Coins"
amnt = 20
WaitTime = 86400
local PlayerArray = {}
local function getPlayerFromCharacter(character)
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character == character then
return player
end
end
return nil
end
function onTouch(hit)
if hit.Parent:findFirstChild("Humanoid") == nil then -- Check that its Humanoid
return false
end
local Player = getPlayerFromCharacter(hit.Parent)-- Make sure it was a player and not some sort of npc
if(Player == nil) then
print("Humanoid wasn't player")
return false
end
if(PlayerArray[Player.UserId] == nil or PlayerArray[Player.UserId] <= tick()-WaitTime) then -- Check if the player needs to wait
PlayerArray[Player.UserId] = tick()
else
print("Player:"..Player.Name.." has to wait ".. (PlayerArray[Player.UserId]+WaitTime)-tick().." more seconds.")
return false
end
Player.leaderstats:findFirstChild(currency).Value = Player.leaderstats:findFirstChild(currency).Value + amnt
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Sound:Play()
wait(1)
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
script.Parent.Touched:connect(onTouch)
You would be better off using Data Stores but this should work for a temporary solution.
I noticed that you made an error in your script.
Error: ThisPlayer.leaderstats:findFirstChild(currency).Value = >ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
Correct: ThisPlayer.leaderstats:findFirstChild(currency).Value = ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
currency = “Coins”
amnt = 20
debounce = false
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
The problem is that you are handing this globally, that means the debouce will be replicated to all clients!
What you can do is handle this in a local script in StarterPlayerScripts:
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()
end
end
PartToTouch.Touched:Connect(onTouch)
And now you must set a normal script, i would recommend you set this is ServerScriptService:
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, add this script in serverscriptservice too:
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)
This script took 10 minutes to do lol, because im lagging, and yeah, i didn’t test it, so if you find any error tell me and i will try to fix it, because i made this very rushed. Thanks for reading
For the last script would I need to make it a local script in serverscriptservice?
I didnt read what you said correctly
And, add this script in serverscriptservice too:
So for my final question, the middle script and the last script are two different scripts that both go into serverscriptservice?
And what I meant buy learning html and css in a week is learning it, and also making one of those responsive websites you see professionals doing as well, and publishing it to github(I could only upload it to github because im broke and I dont have a paypal) But learning lua on the other hand took me about 6 months just to like figure it out and edit parts of the script. So rn I can only read and edit scripts to fit what I need them to do, but for some reason I cant make a script from scratch to do what I want it to do.