Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Make this script only affect who clicked it?

Asked by 8 years ago

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)
0
The value is in workspace, so it's going to affect the value because that's what it's changing. 1waffle1 2908 — 8y

2 answers

Log in to vote
1
Answered by
Relatch 550 Moderation Voter
8 years ago

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.

Ad
Log in to vote
1
Answered by 8 years ago

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)

Answer this question