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

How to change a value through a localscript via a remote event?

Asked by 2 years ago

For example how would I be able to click on a gui button and when I click it a value inside the Gui changes based on the specific criteria?

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

If you only want to change it locally within the script, there would be no need to use a remote event (remote event is used for either: client to client, client to server, or server to client)

If that is all you wanted to achieve, this should work:

local button = *Button Location*
local ValueToChange = *Value Location*

button.Activated:connect(function()
    ValueToChange.Value = ValueToChange.Value (Whatever math you want to use for the criteria)
end)

If, however, you wanted to have it server side, then here is another script, that uses remote events and has leaderstats:

--In a server script (preferably in ServerScriptService)

local Players = game.Players
local RemoteEvent = game.ReplicatedStorage.RemoteEvent --Location of the remote event, preferably put it in replicated storage


Players.PlayerAdded:connect(function(plr)
    local leaderstats = instance.New("Folder",plr)
    leaderstats.Name = "leaderstats"

    local Value = instance.New("IntValue", leaderstats)
    Value.Name = "Value" -- You can change this, but you must change it below as well
end)

RemoteEvent.OnServerEvent:Connect(function(plr)
    plr.leaderstats["Value"].Value = --Whatever math you wish to apply. Additionally, if you change the name in the function above, change it here as well.

end)

Then, In a local script, do:

local button = *Button Location*
local ValueToChange = *Value Location* --The value in the GUI
local RemoteEvent = game.ReplicatedStorage.RemoteEvent --Location of the remote event

button.Activated:connect(function()
    RemoteEvent:FireServer()
end)

game.Players.LocalPlayer.leaderstats.Value.Changes:connect(function()
    ValueToChange.Value = game.Players.LocalPlayer.leaderstats.Value.Value
end
Ad

Answer this question