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

What is the best way to have a variable that everyone has to alter individually?

So I have these coins, right? They act as collectibles that players can get by touching, and once they do, it becomes transparent to signify that they already got that coin. The coins remain nontransparent until you collect it yourself and when you do, it is added to the coin count inside of the coin count GUI.

However, there is one problem with this script. It's that the boolVals for each coin that changes for everyone once one player accesses it. This makes it so a player who comes in after a previously collected coin and collects it will add to their coin count by 15+ because there is no denounce that says they can't collect it again.

I expect this sort of thing, but I need to find an easy solution for this problem. Right now, I am thinking about creating 7 boolean instances in the players so that everyone has their own boolVal they can alter. However, I feel as if this is a very messy way to go about this, and I'd like to know if there is anything better that I can do. If so, please let me know in detail as I am not very experienced in coding.

  • FilteringEnabled is on
  • the script is a local script inside of StarterGui
local coinTrack1 = true
local coinTrack2 = true
local coinTrack3 = true
local coinTrack4 = true
local coinTrack5 = true
local coinTrack6 = true
local coinTrack7 = true
local coinNum
local CoinTrackerLabel = game.StarterGui.CoinTracker.Frame.TextLabel.Text
if coinNum == nil then
	coinNum = 0
end
game.StarterGui.CoinTracker.Frame.TextLabel.Text = "Coin "..coinNum.."/7"
game.Workspace.RedCoin1.Touched:Connect(function(hit)
    if coinTrack1 == true then
        if hit.Parent:WaitForChild("Humanoid") then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if coinNum == nil then
				coinNum = 0
			end
            coinNum = coinNum + 1
            player.PlayerGui.CoinTracker.Frame.TextLabel.Text =  "Coins "..coinNum.."/7"
            game.Workspace.RedCoin1.Transparency = .85

            coinTrack1 = false
            return coinNum


        end
    end

end)
-- Then there is a function identical to the above one for each Coin

(This is a repost from last night because I feel as if I posted too late at night for anyone to answer.)

2 comments

0

get rid of local when subtracting or whatever

0

Get rid of it from the beginning and just make it a global variable? Or do you mean setting it as a local variable inside the function.

1 answer

Try checking if the transparency of the coin is 0.85. If it is, then the function will not apply.

local coinNum
local CoinTrackerLabel = game.StarterGui.CoinTracker.Frame.TextLabel.Text
if coinNum == nil then
	coinNum = 0
end
game.StarterGui.CoinTracker.Frame.TextLabel.Text = "Coin "..coinNum.."/7"

if game.Workspace.RedCoin1.Transparency == .85 then
	return
else
	game.Workspace.RedCoin1.Touched:Connect(function(hit)
        	if hit.Parent:WaitForChild("Humanoid") then
        		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
           		if coinNum == nil then
                		coinNum = 0
            		end
            		coinNum = coinNum + 1
            		player.PlayerGui.CoinTracker.Frame.TextLabel.Text =  "Coins "..coinNum.."/7"
            		game.Workspace.RedCoin1.Transparency = .85

            		coinTrack1 = false
            		return coinNum
		end
	end)
end
-- Then there is a function identical to the above one for each Coin

9 comments

0

good idea

0

Okay, so I added this exact function in the script for each coins, and now it acts like it doesn't have a debounce and everyone's coin count goes up 15+. I also altered the script in some areas afterward like changing the .85 in "if game.Workspace.RedCoin1.Transparency == .85 then" to 0, but then the player wasn't able to pick it up at all.

0

The transparency is 100 nontransparent, or 0, so i am not sure why this wasn't successful.

0

I edited the script after you saw it because I made a mistake which could have affected the whole thing, so I'm not sure if you used the original script or the edited script.

0

I'll try it again

0

Okay, so now it won't let the player collect it at all.

0

Oof, sorry I don't know how else to help you. All I can think of is checking the transparency before the touched event. That way, the function for the touched event cannot run unless the coin isn't transparent.

0

I'll try that, thank you.

0

It did the same thing. I'll do some more research and I'll see what I can do.

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