Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to +1 to leaderstats on imagebutton clicked?

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

✓ Accepted answer

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.