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

How to make a remote event to change any parameter in the datastore?

Asked by 5 years ago
Edited 5 years ago

I have this in a local script:

ReplicatedStorage:WaitForChild("ChangeParam"):FireServer(game.Players.LocalPlayer.data.has_done_intro.Value, true)

and this in the server script:

ReplicatedStorage.ChangeParam.OnServerEvent:Connect(function(player, param, new)
    param = new
    save_data(player)
end)
-- save_data(player) just saves the datastore.

I suppose this code doesn't work because the change is done in the local script, and not the server script (where it should be done in order for save_data to work). My goal is to be able to change the user's variables (in the folder leaderstats and a folder I created called data) from any local script, without having to make separate remote events for every single thing I want to change. Is there a way to do that? Maybe I should stick to separate remote events for each parameter (such as changing coins, level...)?

1 answer

Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago
Edited 5 years ago

Hello KamptonGames! To do this, you will need to fire a remote event with what you want to be changed and the new value! Let's get started:

SETUP

1) Create a remote event under replicated storage

-Name the event "ChangeData"

2) Make a server script under ServerScriptService

-Name it "DataMain"

3) Make a local script and put it wherever you need to fire the dataChange

-Name it at your discretion

SCRIPTING

In the DataMain Server script:

ChangeEV = game:GetService("ReplicatedStorage"):WaitForChild("ChangeData")

ChangeEV.OnServerEvent:Connect(function(plr, dataName, dataValue)
    local toChange = plr.leaderstats:FindFirstChild(dataName)
    toChange.Value = dataValue
end)

In the local script, you can put this line of code whenever you want to change data:

game:GetService("ReplicatedStorage"):WaitForChild("ChangeData"):FireServer(*name of data here*, *new value for data here*)

With that said, of course the data name would actually have to exist in leaderstats, and the new value should equal the data type. But I hope this helped! With any further questions/comments/concerns, please feel free to comment on my answer! ~mc3334

0
Brilliant. KaptonGames 11 — 5y
0
But remember something EXTREMELY IMPORTANT. That code is EXTREMELY VULNERABLE. Exploiters can destroy literally EVERYTHING AND ANYTHING in your game if you do that. sweettart13947 105 — 5y
0
Oh! How can I make it not vulnerable? KaptonGames 11 — 5y
0
I suppose clients can send to the remote event and therefore add coins to themselves etc... KaptonGames 11 — 5y
0
Maybe sending some kinda encoded key that only the server can decode will work? Donno, please help. KaptonGames 11 — 5y
Ad

Answer this question