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

How to let players get the money from a coin after another player has touched that coin?

Asked by
Soban06 410 Moderation Voter
4 years ago

So I have a coin in my game. When a player touches the coin, the player get's 1 coin added in their leader stats. But now when another player touches the same coin which another player touched before, they don't get the money and the coin is still there. It's giving money functionality is finished when 1 player touches it and get's their money. But now the others can't get it.

Can someone please help me with this so that the other players successfully get their +1 point after someone else touched that coin? I think we can do this by making the coin locally but I don't understand how.

The coin is a model.

Here is my code:

Collect Script (this is a normal script and not a local script):

local db = true
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        if db == true then
            db = false
            script.Parent.Transparency = 1
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
            script.Sound:Play()
            wait(1)
            script.Parent.Transparency = 1
            wait(1)
            script.Parent.Transparency = 0

        end
    end 
end)

Now the leader board script which adds a coin's table to the leader board;

local coins = Instance.new("IntValue", leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

Note that I have added the player added event in the above script but I haven't shown it since it is not neccessary.

Thanks

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

By the looks of it you have incorrectly setup your debounce variable.

After you set the transparency back to 0 you need to set db back to true. Else your condition for checking debounce will fail.

local db = true
local Cooldown = 10
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        if db == true then
            db = false
            script.Parent.Transparency = 1
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
            script.Sound:Play()
            script.Parent.Transparency = 1
            wait(Cooldown)
            script.Parent.Transparency = 0
            db = true
        end
    end 
end)

Edit: If you want to make it so there is a cooldown between getting coins you can use wait() to fix this.

I've added my modifications to the code above.

0
Thanks for your help. But now the player can repeatedly touch the same coin again and again to get more and more points. Is there a way to stop that same player from touching the same coin he touched before? Soban06 410 — 4y
0
Or maybe you can just destroy the coin from the local player and not from other players. Soban06 410 — 4y
0
I've added a wait and attached a cooldown variable. See my edit. PURGATORYAGENT_1 43 — 4y
0
Apologies I understand what you want now. You could put this in a local script yes. If you put a local script inside startergui and access coins inside of workspace you can bind them to a touched event and fire a remote event to add 1 to leaderstats to do this. PURGATORYAGENT_1 43 — 4y
View all comments (3 more)
0
Is there anyway to destroy the coin from the local player only and not from the rest of the players? Because the player can wait for the cooldown. But if the coin is destroyed locally, that would be much better.  Soban06 410 — 4y
0
Using a local script yes. PURGATORYAGENT_1 43 — 4y
0
The coins are successfully being added to the leader board without the remote event. But I want the coin to be destroyed locally. I wrote the above in a local script inside startergui. But everything is working like before. And the coins are not destroyed locally. Soban06 410 — 4y
Ad

Answer this question