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

How to add a leader stat once when you touch a block?

So I have a coin script that gives you one coin when you step on it. I made a gamepass you can buy where you get 3 coins every time you step on a coin. (A coin trippler). Although I get 9 coins instead of 3.

local db = true
local passId = 297078187

script.Parent.Touched:connect(function(obj)
	local player = game.Players:FindFirstChild(obj.Parent.Name)
	if player and db then
		db = false
		local coins = player.leaderstats.Coins
		workspace.Sounds.CoinSound:Play()
		for i = 0, 1, .1 do
			script.Parent.Transparency = i
			wait()
		end
		local coins = player.leaderstats.Coins
		if game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) then
			coins.Value = coins.Value + 3
		else
			coins.Value = coins.Value + 1
		end
		script.Parent:Destroy()
		db = true
	end
end)
---put this in workspace

Any idea how this happens? Thanks.

2 comments

0

I don't think there is a error and you can touch the coin giver a lot of times so 9 coins wouldn't be a problem in my opinion.

0

Yes I see that but when I touch it once it gives me nine.

1 answer

You should've done local db = false I also Reccomend WaitForChild()

local db = false
local passId = 297078187

script.Parent.Touched:connect(function(obj)
    local player = game.Players:GetPlayerFromCharacter(obj)
    if player and db == false then
        db = true
        local coins = player.leaderstats.Coins
        workspace.Sounds.CoinSound:Play()
        for i = 0, 1, 0.1 do
            script.Parent.Transparency = i
            wait()
        end
        --local coins = player.leaderstats.Coins Why declare the same variable twice??
        if game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) then
            coins.Value = coins.Value + 3
        else
            coins.Value = coins.Value + 1
        end
        script.Parent:Destroy()
        db = false
    end
end)
---put this in workspace

Hopefully this would help you in this script :)

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