Have a script where adds +1 to gui when touching coin. After touching it once though i need it destroyed

Have a script where adds +1 to gui when touching coin. After touching it once though i need it destroyed. Did the basic destroy script but not working.
Server Script service script:
image
Coins GUI script


Starter character scripts local script

What exactly do you want destroyed? The coin, the script, the points, the ability of the coin to add to the next person touching it?
Also, hard to read scripts where you use variables that are the same name as the item they represent.
For example:
local coins = Player:waitForChild("leaderstats").Coins
so that you can do
coins.Value = coins.Value +1

I want the coins to get destroyed when humanoid touches part.

  1. I hope you know that studio has a dark mode, it reduces strain on the eyes by alot.
  2. You can save lots of time by shortening arithmetic statements, for example, x = x + 1 can become x += 1, and a = a * b can become a *= b
  3. Never use a while true do loop with a wait() inside. It is a terrible mistake and you will find out why when you learn more about Luau.
  4. You are trying to access Coins in the TextLabel Coins in your .Changed connection in the GUI script. You just do script.Parent.Text.
  5. Instead of disabling the script and making the coin transparent, you can literally just do Coin:Destroy(). This makes all references to it nil however, so make a presence check before you do anything with it.
  6. You should ALWAYS get services in your scripts. Sometimes the service will not exist yet, so you should do game:GetService() to guarantee it will be available.
  7. Make a function to destroy and create a coin in the StarterCharacterScripts script, just to make it more maintainable and readable.
  8. The script in StarterCharacterScripts should preferably be in StarterPlayerScripts, as it will otherwise reset when the character resets.
1 Like

Instead of making the coin transparent, then having Coin.CanTouch = false set to true, why not just use Coin:Destroy()

Make this a script inside of the coin (server script)

local coin = script.Parent

coin.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent) or game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
    
    if plr then
        plr.leaderstats.Coins.Value += 1
        coin:Destroy()
    end
end)

In a local script, it won’t actually add to the coin value.

1 Like

Thank you all, i will try right now, thanks for your time.

Thank you so much it worked!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.