I don't know how to explain this well but I will try my best.
I have this example script:
game:GetService("Players").PlayerAdded:Connect(function(player) game:GetService("Workspace").Part.Touched:Connect(function(hit) local Money = player.leaderstats.Money Money.Value = Money.Value + 1 end) end)
If the Player1 join the game(player now is Player1), but he/she not touching the part yet(So Player1 doesn't get any money). Next time, Player2 join the game(player now is Player2) and Player1 start to touching the part, what will happen?
Will Player1 get money because Player1 touch the part, or Player2 get money because player now is Player2? If Player2 get money then is there any way to stop that happen?
Thank for reading :D
Why don't you just do something like this?
local Workspace = game:GetService('Workspace'); local Players = game:GetService('Players'); local Money_part = Workspace:WaitForChild('Part'); local Touch_settings = { Delay = 4; Players = {}; }; Money_part.Touched:Connect(function(Touched) local Player = (Touched.Parent:IsA('Model') and Players:GetPlayerFromCharacter(Touched.Parent)); if Player then local Player_delay = Touch_settings.Players[Player]; local Leaderstats = Player:FindFirstChild('leaderstats'); if Leaderstats and (Player_delay and (tick() - Player_delay > Touch_settings.Delay) or not Player_delay) then local Money = Leaderstats:FindFirstChild('Money'); if Money then Money.Value = Money.Value + 1; Touch_settings.Players[Player] = tick(); print(string.format('%s\'s money has increased to %s$', Player.Name, Money.Value)); end; end; end; end);
You can simply do this by creating an event in ReplicatedStorage and naming the Event MoneyEvent and then Fire it whenever the part is touched. I already added the fire event so, you just need to need to create the event in ReplicatedStorage. This is my way but there is probably a lot of other ways.
Keep in mind, that RemoteEvents can be exploited and could be used to change their Money, I only made it so the player who touches gets the money.
game:GetService("Players").PlayerAdded:Connect(function(player) game:GetService("Workspace").Part.Touched:Connect(function(hit) local Money = game.Players[player.Name].leaderstats.Money game.ReplicatedStorage.MoneyEvent:FireClient(player, hit, Money) if player.Name == hit.Parent.Name then Money.Value = Money.Value + 1 print("Money has been given to "..player.Name.." and no one else.") end end) end)
Also, this script increases the money for the person who touched the part. I couldn't quite understand but I hope this helped you.