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

im trying to like if you have 1 cash you can buy 1 ingridients please help me?

in my screen gui there is a button and in the button there is a local script

script.Parent.MouseButton1Click:Connect(function()

	local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	
	if plr.leaderstats.Cash >1 then
	 plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value -1
    plr.leaderstats.Ingridients.Value = plr.leaderstats.Ingridients.Value +1
   end

end)

1 comment

1 answer

✓ Accepted answer

For one, you would need a RemoteEvent because if you just do it from that LocalScript it won't replicate to the server, which you want to happen.

Inside of ReplicatedStorage insert a RemoteEvent and name it whatever you want. You'll just have to put the name inside of the script below.

Inside of the LocalScript put this:

local player = game.Players.LocalPlayer -- Instead of doing script.Parent.Parent, you can do this.
local RemoteName = "hi" -- Replace this with the name of your remote.

local RS = game:GetService("ReplicatedStorage") 

local ScreenGui = script.Parent -- Change this to the path of your ScreenGui
local TextButton = script.Parent.TextButton -- Change this to the path of your TextButton

TextButton.MouseButton1Click:Connect(function()
	RS:WaitForChild(RemoteName):FireServer()
end)

Once you've done that we need to make a Script inside of ServerScriptService. Once you insert one you can put the following code:

local RS = game:GetService("ReplicatedStorage")
local RemoteName = "hi"

RS:WaitForChild(RemoteName).OnServerEvent:Connect(function(player)
	if player:WaitForChild("leaderstats").Cash.Value >= 1 then
		player:WaitForChild("leaderstats").Cash.Value = player:WaitForChild("leaderstats").Cash.Value - 1
		player:WaitForChild("leaderstats").Ingridients.Value = player:WaitForChild("leaderstats").Ingridients.Value + 1
	end
end)

4 comments

0

I just cant seem to make it work can u add me and help me fix it? username: thecool_leoanrdokh

0

Do you have Discord? If so, what is it?

0

LeoYT#9077

0

Can I have some help again?

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