Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is the new Player instance will overwrite the old Player everytime PlayerAdded fired?

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

2 answers

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);

3 comments

0

Your solution is very clever, but there is a problem. In line 13, Player_delay would be nil, because there is no Player index in Players.

0

There isn't one, its fine though that's why there is this "(Player_delay and (tick() - Player_delay > Touch_settings.Delay) or not Player_delay)" it doesn't matter if it is already indexed or not it will be added after.

0

So basiclly, the statement is just "if Leaderstats and not Player_delay then", because the Player will never be index.

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.

2 comments

0

exploiters could still fire this repeatedly to gain money, you'd have to check how close they are using magnitude

0

Mhm.

This discussion is preserved as it was. New answers, comments, and votes are closed.