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

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

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

✓ Accepted answer

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

5 comments

0

Brilliant.

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.

0

Oh! How can I make it not vulnerable?

0

I suppose clients can send to the remote event and therefore add coins to themselves etc...

0

Maybe sending some kinda encoded key that only the server can decode will work? Donno, please help.

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