When i touch a part, sometimes it disappears and adds +1 to GUI but sometimes it doesn’t disappear or add 1 to GUI. I want it to disappear
Theres a couple mistakes in your script.
- You should be using a Server Script because when you use a local script the Coin data will only change for your client.
- You dont check if a player touched the coin, you just check if it was touched by any part.
Heres a fixed script you should paste into a Server Script in ServerScriptService:
local Players = game:GetService("Players")
local Coin = workspace.Coin
Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new("Folder", Player) --// Create folder that stores the Coin data.
Stats.Name = "leaderstats"
local Coins = Instance.new("NumberValue", Stats) --// Create a NumberValue that will store the amount of coins the player has.
Coins.Name = "CoinAmount"
Coins.Value = 0
end)
Coin.Touched:Connect(function(Hit)
if (Hit.Parent) and (Hit.Parent:FindFirstChild("Humanoid")) then --// Checks if the hit part is a character.
local HitPlayer = Players:GetPlayerFromCharacter(Hit.Parent) --// Get the player that touched the coin.
HitPlayer:WaitForChild("leaderstats").CoinAmount.Value += 1 --// Add 1 to the players coins value.
Coin:Destroy() --// Destroy the coin.
end
end)
Looking at the explorer, seems like you have multiple coins in the workspace, but the variable is only leading to one of them, this means that you can actually touch a coin and nothing happen because the Coin variable isn’t actually pointing to it!
Thank you fixed! Have a great day!

