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

How do I create a clickdetector script then when clicked gives the play oh-so money?

Asked by 6 years ago

What I'm doing is create a part with a clickdetector in it. I am then creating a script inside of the clickdetector that registers when the player clicks the part and gives them oh-so much money, 500 for example.

I don't want to include my failure scripts here as I would only be embarrassed. I'm also pretty sure that I will need a completely new script and I am going in the opposite direction.

2 answers

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 years ago

Edit: The ClickDetector Script was not working because I forgot the bracket at the end. It should be fine now.

Don't be afraid to put your scripts on Scripting Helpers, as we are all here for a reason, whether as askers or answerers. To make this, you would simply connect a function to the MouseClick event of ClickDetectors.

script.Parent.MouseClick:Connect(function()
    -- code
end

The first parameter is the player who clicked it. From there, you can go to the player's leaderstats and add 500 to the value named "Money".

Put inside ClickDetector

script.Parent.MouseClick:Connect(function(player) -- player that clicked. It comes with the event.
    if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Money") then
        player.leaderstats.Money.Value = player.leaderstats.Money.Value + 500 -- add 500 to money.
    end
end) -- I forgot the bracket here.. my bad.

This is assuming that there is already a value inside the player named "leaderstats" and inside that, there is a value named "Money" If not, then put this script inside ServerScriptService.

game.Players.PlayerAdded:Connect(function(player)
    local l = Instance.new("Folder")
    l.Name = "leaderstats"
    local m = Instance.new("IntValue")
    m.Name = "Money"
    m.Parent = l
    l.Parent = player
end)
0
Instead of having the value called "Money" I have it called "Cash" so I am assuming I would change FindFirstChild("money") to ("Cash") and the same for the others? HeadlessGuide 16 — 6y
0
Im testing this theory now ^. HeadlessGuide 16 — 6y
0
If the value is called "Cash" then change wherever it says "Money" and put "Cash" instead. UgOsMiLy 1074 — 6y
0
I have tried that but for some reason it seems to not be working. I do put the script inside the clickdetector right? HeadlessGuide 16 — 6y
View all comments (11 more)
0
This is the script inside the part inside the clickdetector: https://imgur.com/a/UI6MY HeadlessGuide 16 — 6y
0
And these are when Im in test mode inside my player https://imgur.com/a/4Rmcu HeadlessGuide 16 — 6y
0
That's because there are multiple "leaderstats" in your player. The script takes only one, and sees that there is no "Cash" inside. Did you use the last script I put in the answer? If so, then remove all the other code that creates "leaderstats" in your game. UgOsMiLy 1074 — 6y
0
There might be other scripts in your game that create leaderstats in the player, and they might not be doing it only once. UgOsMiLy 1074 — 6y
0
Should I remove the ones that are creating more leaderstats in the player and use your one instead? I can see how the multiple ones will confuse the script HeadlessGuide 16 — 6y
0
Probably; Try that. UgOsMiLy 1074 — 6y
0
Alrighty HeadlessGuide 16 — 6y
0
I deleted the ones creating leaderstats in the player and switched to your ones. Now my tycoon money system has broken. I need to go now so I will look at this later. Thanks for the help so far though. HeadlessGuide 16 — 6y
0
Wait.. I just tested this in studio, and it turns out that the ClickDetector Script errors due to the fact that I forgot to put an end bracket on the last line. I've fixed it now. UgOsMiLy 1074 — 6y
0
Alright im going to revert my scripts back from the tycoon and then add the end bracket on. HeadlessGuide 16 — 6y
0
Alright the script works now. Thanks for your help! HeadlessGuide 16 — 6y
Ad
Log in to vote
0
Answered by
Kegani 31
6 years ago

I think this should work.

game.Players.PlayerAdded:connect(function(plr)
    local s = Instance.new("Folder", plr)
    s.Name = "leaderstats"
    local c = Instance.new("IntValue", s)
    c.Name = "Cash"
    c.Value = 0
    script.Parent.ClickDetector.MouseClick:connect(function()
        c.Value = c.Value + 500
    end)
end)

Insert a part, put this script in it. Make sure it features a ClickDetector.

Answer this question