I'm trying to make it when this apple is clicked, it adds 1 to the money value, and deletes itself, but make it only affect the one who clicked it.
Code :
function onMouseClick(player) game.Workspace:FindFirstChild("MoneyVal").Value = game.Workspace:FindFirstChild("MoneyVal").Value+1 print("Money collected") script.Parent:Destroy() print("Apple harvested") end script.Parent:FindFirstChild('ClickDetector').MouseClick:connect(onMouseClick)
Like BosswalrusTheCoder said, you need to use a leaderboard or add the value to the player when they enter the game. I noticed in chat you said you don't want it to show a leaderboard, so i'll answer it for you.
game.Players.PlayerAdded:connect(function(Player) local Money = Instance.new("IntValue", Player) Money.Name = "Money" end)
Now that we have added the IntValue named "Money" to the player, it's time to handle the clicking script.
local Apple = script.Parent local Clicker = Apple.ClickDetector local Worth = 1 Clicker.MouseClick:connect(function(Player) local Money = Player:FindFirstChild("Money") if Money then Money.Value = Money.Value + Worth Apple:Destroy() end end)
Also, I removed the line that returns "Worth" to 0. If this breaks the script, or makes it not run how you want it, just add it back under line 9. You only need to delete the apple, not return the "Worth" value to 0.
First let's make a leaderstats script. Insert a Script into ServerScriptService
game.Players.PlayerAdded:connect(function(Player) local stats = Instance.new("IntValue", Player) stats.Name = "leaderstats" local Money = Instance.new("IntValue", stats) Money.Name = "Money" end)
Now we've made our Money value, it will appear on the leaderboard. Put this script in the Apple:
local Apple = script.Parent local Clicker = Apple.ClickDetector local Worth = 1 Clicker.MouseClick:connect(function(Player) local stats = Player:findFirstChild("leaderstats") local Money = stats:findFirstChild("Money") if Money then Money.Value = Money.Value +Worth Worth = 0 Apple:Destroy() end end)