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

Why isn't my localscript changing a global value?

Asked by 3 years ago

I'm trying to make a money and token system, and everything works, except that the changes that my localscript in my shop make to the leaderstat values stay local. So for example, when i test it, if I have 547 money, and 0 tokens, and I buy 6 tokens, the changes appear on my screen as the client. However, when I go into the server, it says that player Sergeant_Ranger's leaderstats are still 547 money and 0 tokens. I'm assuming that the problem is that i'm using a localscript, but how would I fix that (as my localscript is in a screengui)? And why does a localscript not change values globally?

Also, no error messages pop up in my output

(btw, if the scripts are needed, comment that; i didn't think they were necessary, and it would be quite long)

1
Hey, are you processing transactions on the client? If so, that is a very bad idea. Kevin82951817 16 — 3y
0
huh? sergeant_ranger 184 — 3y
0
i am processing transactions on the client beacuse i dont know any other way to do it from a screen gui. Is there any other way to do it? sergeant_ranger 184 — 3y
0
Maybe you should read up on RemoteEvents and RemoteFunctions, they help prevent your game from being hacked. If you process transactions on the client, then hackers could manipulate their own client to see what they need to do to get the money. So you should use a RemoteFunction and pass the data inputted by the client. You can read more on the official ROBLOX developer website. 0neDeletedUser 42 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

Remotes!

A RemoteEvent or a RemoteFunction may be what you are looking for. As I said earlier in a comment for your code, you need to edit server-side things with Remotes. So one thing that you need to do first is add a RemoteEvent to your ReplicatedStorage section. This can only be in ReplicatedStorage, as it is the only thing that the client can globally change without FilteringEnabled blocking it.

Next thing to do is give it a name, and start your script. You could probably put your script in ServerScriptService, but it should work the same no matter where it is placed, under the condition that it is in a service accessible by the server only.

From here, we'll start coding our script that we just placed. To get started, I'm sure you have at least a little coding experience. So we will start off with a basic function opener. Note that we will use two arguments, one being the player object, and the amount of gain they will get.

local ReplicatedStorage = game:GetService('ReplicatedStorage')

ReplicatedStorage[name of the remoteevent].OnServerEvent:Connect(function(plr,affectInt) 
    --Anything in this function will run when the RemoteEvent is called. It may seem weird right now, but it will all come together soon.
end)

(ALERT: Remember to ALWAYS put the argument for the player or else your code will not work when handling RemoteEvents/Functions on the server-side!)

Next, inside of that function, we need something to actually change the leaderstats for the player. So what we will do is write some code to edit the player's leaderstats. We don't need to use a RemoteEvent, because the 'Players' service is a server-side service when it comes to writing data. You can still read from either side.

    game:GetService('Players')[plr.Name].leaderstats.Tokens = game:GetService('Players')[plr.Name].leaderstats.Tokens + affectInt

Bang! We've finished the whole server-side replication system! Now, we just need to add something to call the function on the client side.

Let's start off by manipulating the LocalScript slightly. I'm sure you have this line of code, or something similar it. Whatever line of code changes the token amount, you need to replace it. Here is an example of what it may look like:

game.Players.LocalPlayer.leaderstats.Tokens = [new amount]

So delete that, and now what we have to do is make a function to call the server-side RemoteEvent. (Yes, I know that I have said that 50 times. I will probably say it again.) Define your variables, such as the actual RemoteEvent and the ReplicatedStorage service.

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TgtRemoteEvent = ReplicatedStorage[name of the remoteevent]

Great! Just one last step, to fire the event. This is the last line, I promise. The way to fire an event like this is similar to a function. We can make use of FireServer() to do so.

Therefore, let's fire the server and input our arguments! The first argument, plr, does not have to be defined as it is already written when the thing is fired. The only thing that we need to write as an argument is the integer it will change by. What I mean, is we don't want to put in a new value, we just want to show how much a person gains/loses. So if they spend 6 tokens, our integer would be -6 (take away 6 of their tokens.) If they gain 6 tokens, then the integer would be 6 (add six tokens.) Alright, moving onto the code.

TgtRemoteEvent:FireServer(6)

...and we are done! It all should work now. If you have any questions, comments, concerns, or problems, please leave a comment and I will try my hardest to answer anything. If your question is out of my league, you may need to get someone else. Other than that, happy coding! :D

0
Thank you, this helped a lot! sergeant_ranger 184 — 3y
0
Np! Enjoy :D 0neDeletedUser 42 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

ok so in a local script it only makes changes to one person so that one person would be the only person that would see the change so what you have to do is use a remote event to make the change in a regular script if you don't know how to use a remote event look here if you still don't understand it tell me remote event are very important and useful

0
how do i pass variables through a remote event (like the userID of the person who bought the tokens and how many they bought)? sergeant_ranger 184 — 3y
0
like an integer, how do i pass that through a remote event? sergeant_ranger 184 — 3y
0
so you would have a remote event in replecatedstorage and in the client you would do game.replecatedstorage.remoteevent:fireserver(amountoftokentshere) botw_legend 502 — 3y
0
then in the server you would do game.replecatedserver.remoteevent.onserverevent:Connect(function(player,numoftokents)--the player who fired it is default the first one botw_legend 502 — 3y
View all comments (2 more)
0
             code here: botw_legend 502 — 3y
0
end) botw_legend 502 — 3y

Answer this question