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

Make a leaderstat go up when you click a part?

Asked by 2 years ago

Hi, i want a leaderstat to go up when you click a part Here is my leaderstat script

local Players = game:GetService("Players")

local function leaderboardSetup(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local gold = Instance.new("IntValue")
    gold.Name = "Snow"
    gold.Value = 0
    gold.Parent = leaderstats
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

Thanks! Cameron

0
Also, anything that says "gold" has been changed to snow. TheRobloxGamerYTy 2 — 2y
0
Yes, you named it "Snow", instead of "gold". Just name the local "Snow" to make it less confusing. I think you're following off a tutorial, which is fine..But you need to be careful with how you write code. Antelear 185 — 2y

3 answers

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago

To make the snow stat go up for the player name TheRobloxGamerYTy, you should do:

local Players = game:GetService("Players")

Players.TheRobloxGamerYTy.leaderstats.Snow.Value += 1

This will only add your value by once, and if the player isn't named TheRobloxGamerYTy, it won't make the snow stat goes up for him because the script above tries to make the player TheRobloxGamerYTy snow stat goes up but he isn't TheRobloxGamerYTy? Don't worry, and the script above obviously can't be the solution because it's not what we wanted:

Let's talk about how do you set up a brick that you can click it to make it do something:

Firstly, you add a pointy finger and it's named ClickDetector (Hopefully you know how, but if you don't, hover your mouse to the brick with your mouse in explorer, it will appear a plus (+) button, click it and search for something called ClickDetector and click the pointy finger icon or the text besides it. It will look like this).

If you understand what I mean, you must know how to do this step! The objective now is to add a script inside the ClickDetector, and edit the script and let it be:

local ClickDetector = script.Parent

local function DetectorClicked(PlayerWhoClicked)
    PlayerWhoClicked.leaderstats.Snow.Value += 1
end

-- Connect the "DetectorClicked()" function to the "MouseClick" event
ClickDetector.MouseClick:Connect(DetectorClicked)

If you understand how the first example code I gave and the leaderstat script, you might understand what does this do or barely, let's explain how will it work

  • First, it will set up the MouseClick event and make it connect to DetectorClicked() event (or function, as you can see it besides the keyword DetectorClicked() in the code)

  • Second, we will need something called a "parameter", and the parameter in the DetectorClicked() event is PlayerWhoClicked, if you look at the script, you would find that PlayerWhoClicked is inside the () in the DetectorClicked() event, and that is a parameter, I won't explain more but what parameter does is give information as the parameter in the DetectorClicked() event will give the player that clicked the ClickDetector, also you can rename it something else you want but the information won't change

  • Lastly it gives the player who clicked one snow in the value

Why is this long? Because other tutorials I find is lame and just throw random professional words to you and want you to copy and paste their code (please don't, but this is really a tutorial so it's fine)

That's it, if you are confused then tell me below :lol:

0
Ehhhh...Made it a bit TOO complicated... Antelear 185 — 2y
Ad
Log in to vote
0
Answered by
Antelear 185
2 years ago

it's simple to understand:

local Part = script.Parent -- assuming you put the script in the part
local ClickDetector = Part.ClickDetector -- also assuming you put it inside the part.

local function Clicked(person_who_clicked)
    person_who_clicked.leaderstats.Gold.Value += 1 -- += means adding on to your existing value.
end

ClickDetector.MouseClick:Connect(Clicked) -- if it's clicked, then connect the function to this event. (the person_who_clicked variable is replaced by default with the player value.

I hope this helped you, if you're confused or stuck come to me!

Log in to vote
0
Answered by 2 years ago

It's simple! SETUP: Create a Part. In Studio, select the Part and click the plus. Click "ClickDetector". Then, in Studio, select the Part and click the plus. Click "Script". Script:

Part = script.Parent
AmountToAdd = <num> -- put the amount you want to add here (must be a number)
Part.ClickDetector.MouseButton1Down:Connect(function(clicker)
    if clicker then
        clicker.leaderstats.Snow.Value += AmountToAdd
    end
end)

Answer this question