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

How to +1 to leaderstats on imagebutton clicked?

Asked by
dxrrevn 13
2 years ago
Edited 2 years ago

Okay, so I'm making a game based off of Cookie Clicker, so I require a cookie that needs to be clicked that gives you a 'Click' every time the button is clicked.

My script so far is this:

local cookie = script.Parent

cookie.MouseButton1Click:Connect(function()
    game.Players.LocalPlayer.leaderstats.Clicks = game.Players.LocalPlayer.leaderstats.Clicks + 1
end)

It's in a local script, and all lines of the script are correct. My output error is:

Players.dxrrevn.PlayerGui.ScreenGui.MAINBG.Frame.ImageButton.AddCookieScript:4: attempt to perform arithmetic (add) on Instance and number - Client - AddCookieScript:4>

I'm unsure on how to rectify this problem, any help would be greatly appreciated.

Thanks, dxrrevn

2 answers

Log in to vote
0
Answered by 2 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

Well first off, if your using a Server Script (The normal one), change it to a local script as localplayer doesn't work in server scripts and place it under the imagelabel.

Then type this:

local cookie = script.Parent local Player = game.Players.LocalPlayer

cookie.MouseButton1Click:Connect(function() Player.leaderstats.Clicks.Value = Player.leaderstats.Click.Value + 1 end)

you didn't add ".Value" after "Clicks" so you're just changing the leaderstats name instead of its value. This should work, and if it doesn't please reply and I will do it myself

Ad
Log in to vote
0
Answered by 2 years ago

localscript:

local addcookie = game.ReplicatedStorage.AddCookie -- creat remoteevent named AddCookie
local cookie = script.Parent 
local Player = game.Players.LocalPlayer
cookie.MouseButton1Click:Connect(function()
addcookie:FireServer()
end)

serverscript:

local addcookie = game.ReplicatedStorage.AddCookie 
addcookie.OnServerEvent:Connect(function(player)
player.leaderstats.Clicks = player.leaderstats.Clicks + 1
end)

this code will fire from client to server because when you use local script it didnt save your click number . This code will work perfect.

Answer this question