Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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);
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. Block_manvn 395 — 4y
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. advancedev 70 — 4y
0
So basiclly, the statement is just "if Leaderstats and not Player_delay then", because the Player will never be index. Block_manvn 395 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

0
exploiters could still fire this repeatedly to gain money, you'd have to check how close they are using magnitude SKOSHILOKI 67 — 4y
0
Mhm. iiMxtt_Destinyii 62 — 4y

Answer this question