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

How can I make an ImageButton give me + 1 cash on my leaderstats?

My leaderstats script:

game.Players.PlayerAdded:Connect(function(Player)
	local Stats = Instance.new("Folder",Player)
	Stats.Name = "leaderstats"
	
	local DiceRolls = Instance.new("IntValue",Stats)
	DiceRolls.Name = "Dice Roll's"
	DiceRolls.Value = 0
end)

3 answers

✓ Accepted answer

Is it simple, I will make for you a FE for prevent hacking!

First you need to make a Remote Event in ReplicatedStorage

Now Insert a Local Script in the ImageButton and enter the code below :

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
       if not debounce then
          debounce = true
            game.ReplicatedStorage.RemoteEvent:FireServer()
      wait(1)
          debounce = false
end
end)

Now insert a Script in Server Script Service and type this below:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
       plr.leaderstats.Points.Value =  plr.leaderstats.Points.Value + 1 -- Change points to your currency!
end)

Try this, if any issue , reply me!

4 comments

0

This doesn't seem to add 1 to my leader stats. Every single script i have tried hasn't either. I can let you help me in Roblox Studio if you would like.

0

You have to friend me in Roblox though

0

Send me, I will accpect and solve for you when I’m break!

0

Thank you!

Alright like @OriginalDevelops said, you need a RemoteEvent to let ServerScript and LocalScript communicate.

Therefore, you need to put a RemoteEvent in ReplicatedStorage since the LocalScript cannot access ServerStorage.

In LocalScript (This LocalScript should be placed into the ImageButton):

local Clicked = false -- This is a "debounce". I wrote "Clicked" to avoid confusion.
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
	if Clicked == false then
		Clicked = true
		RemoteEvent:FireServer()
		wait(1)
		Clicked = false
	end
end)

In ServerScript (You can use your original ServerScript in the question you asked):

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

game.Players.PlayerAdded:Connect(function(Player)
	local Stats = Instance.new("Folder",Player)
	Stats.Name = "leaderstats"

	local DiceRolls = Instance.new("IntValue",Stats)
	DiceRolls.Name = "Dice Roll's"
	DiceRolls.Value = 0
	
	local Cash = Instance.new("IntValue", Stats) -- Creating a new Cash value in leaderstats
	Cash.Name = "Cash"
	Cash.Value = 0

	while wait() do -- Im using while loop here so that the function can be used several times instead of 1 time.
		RemoteEvent.OnServerEvent:Connect(function(Player)
			Player.Stats.Cash.Value = Player.Stats.Cash.Value + 1
		end)
	end
end)

6 comments

0

Do I put the last script as my leaderstats script , or do I make this script the script I have for OriginalDevelops last script?

0

your leaderstats script

0

Ok.

0

Now there are no leaderstats at all so I can't determine if this will work or not.

0

No leaderstats? You need to add the RemoteEvent to ReplicatedStorage before you test the game or else the leaderstats won't appear

0

They still dont

I have fixed it! All i did was watch this youtube video: https://www.youtube.com/watch?v=FZH8kXC4V2o

My script now:

local Player = game.Players.LocalPlayer
local PlayerStats = Player:WaitForChild('leaderstats'):WaitForChild('Dice Rolls')

script.Parent.MouseButton1Click:Connect(function()
	PlayerStats.Value = PlayerStats.Value + 1
end)

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