How do i fix this script! i thought it would be easy

so, its my first time doing scripts like theys, but i thouhgt i was doing it right, but for some dumb reason, its not working.

here is the script

local amount = script.Parent.Parent.Parent.Amount.Value
local player = game.Players.LocalPlayer.leaderstats

script.Parent.MouseButton1Click:Connect(function()
	if player.Cash.Value >= amount then
		player.Cash = player.Cash - amount
		player.Areas = player.Areas + 1
	end	
end)

If this is a server script, you can’t do local player.

This might not be the direct issue but I recommend using script.Parent.Activated opposed to script.Parent.MouseButton1Click if you want to support mobile devices, I’ve tried using the latter and it usually poses many issues with button clicking since mobile devices don’t generally use mice.

LocalPlayer is a client-only property. You cant get it on the server.

ohhhh thanks, i never new that! BTW so could i do a normal script then and make it only do it for that one person

or, what if i were to do the button in a local script and the buy button in a normal script!
would that work?

Inside of the if-then statement, add the Value.
(player.Cash.Value)

if player.Cash.Value >= amount then
player.Cash.Value = player.Cash.Value - amount
player.Areas.Value = player.Areas.Value + 1
end

Perhaps you may have already fixed this, but it’s what would cause error.

Use this:

local amount = script.Parent.Parent.Parent.Amount.Value

script.Parent.MouseButton1Click:Connect(function(player)
	if player.leaderstats.Cash.Value >= amount then
		player.leaderstats.Cash = player.Cash - amount
		player.leaderstats.Areas = player.Areas + 1
	end	
end)

sorry that didnt work :frowning:

Its best practice to listen for events on the client and then fire a remote to the server.

MouseButton1Click is a Client-sided event therefore it can only be listened for and activated in LocalScripts. You can update leaderstats through the client but it will not update for anyone else. Instead, you should be updating the leaderstats values through a ServerScript. Best way to achieve this is by using RemoteEvents. If you are unfamiliar with RemoteEvents, I recommend you check out this post or this post on the Roblox Developer Hub. If you still need help, I can provide an example of how you would use them for your script.

i have never done mouse clicks

ive never used a remote event, ive tryd so many times in the past, but never worked for me. so what if i were to make it so if the button gets clicked it just makes a part Anchored or something and the part will be in IDK in Lighting, and like if the button is clicked it turns Anchored off and in a normal script ill just put if the part Anchored = false then bla bla bla

would that work?

In order to perform anything on the server, such as updating values, anchoring parts, or anything else, you will need to use RemoteEvents. They are something that are really essential to learn in programming on Roblox. The two posts I linked above are very helpful for people who are unfamiliar, but you could also find tutorials on YouTube if you’re still struggling.

1 Like

This is how you would use remoteevents to tell the server to remove cash/add an area.

-- server script in serverscriptservice
local Players = game:GetService("Players")
local clickRemote = path.to.remote

local amount = 1 -- it's better to just make a variable than have a value for it
local playerStats = {}

local function debounce(func)
    local running = false
    return function(...)
        if not running then
            running = true
            func(...)
            running = false
        end
    end
end

Players.PlayerAdded:Connect(function(player)
   playerStats[player] = player:WaitForChild("leaderstats")
end)

clickRemote.OnServerEvent:Connect(debounce(function(player, button)
    if typeof(button) ~= "Instance" or not button:IsDescendantOf(player.PlayerGui) then
        player:Kick()
    end

    local cash = playerStats[player].Cash
    local areas = playerStats[player].Areas

    if cash.Value >= amount then
        cash.Value -= amount
        areas.Value += 1
    end
end))

-- client (localscript)
local clickRemote = path.to.remote
local button = script.Parent

button.MouseButton1Click:Connect(function()
    clickRemote:FireServer(button) -- tiny bit of added security
end)
1 Like

Combined operators are here btw,

player.Cash.Value -= amount
player.Areas.Value += 1

Exploiters can change values local to them, so that’s not a good idea.

You can listen for the MouseButton events (for some reason not Activated) through a server-script, though it’s not recommended.


@Flaming_Bird22 All you need is either one server-script handling everything for every player (you can listen to MouseButton1Click events from the server if you manage to reference the respective Gui button objects) and increment their values right there, or preferably have a LocalScript listening for client input, fire a RemoteEvent as suggested by others and increment the value on the server for it to replicate to all clients.

Do note that exploiters can take note of this and crash servers after causing code to run too frequently, use some sort of relative debounce to prevent that.

2 Likes

i already have a leader stats i just need it like, you know

It’s better to create it in this script instead of waiting for it since you’re saving the leaderstats folder into a table in this script. If you create it somewhere else, then just combine that script with this one.

This won’t ever update. You should just reference the amount object and check amount.Value inside the event.

3 Likes

yahhh i dont think im that good of a scripter to combine scripts, trust me, you dont know how many times ive tryd, but if you want to do it, not saying im making you do it but if you want to do it then here is the leader stats scripted i used

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CashValueSaver")
local ds2 = datastore:GetDataStore("RebirthsValueSaver")

players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency1 = Instance.new("IntValue")
	currency1.Name = "Cash"
	currency1.Parent = player.leaderstats
	currency1.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency1.Value)
	local currency2 = Instance.new("IntValue")
	
	currency2.Name = "Areas"
	currency2.Parent = player.leaderstats
	currency2.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, currency2.Value)
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)
	currency2.Changed:connect(function()
		ds2:SetAsync(player.UserId, currency2.Value)
	end)
	
end)

your more than welcome to try. but i cant do it :disappointed_relieved: