NumberValue keeps getting overwritten

I’m creating a game with a stats system and more than one action can increase the stats of the player.

For example, I have a sprint script that increases the player’s agility while running (local script in starterplayerscripts, and no issues. I also have an attack script (regular script located in tool within player’s backback) that increases both strength AND agility. Again, no issues.

However, the issues occur when any agility gained by sprinting is completely replaced by agility gained by the attack when fired. Is there a reason why this occurs? I’ve tried converting the scripts between local and regular but nothing seems to be working.

Both lines of code are identical:

player.Stats.AgilityStat.Value += 2

A LocalScript change will not be Replicated across the Client/Server Boundary, as the Attack Script is done on the Server, the Server will be aware of this change, and will be able to apply the Data Appropriately, as for the Sprinting Points, the Server is unaware of this change, only the Client is, because the Server isn’t aware of this change, it will only apply the Change that was done by the Attack, and not the Sprinting.

The Simple Solution would just be to tell the Server about the Change using a RemoteEvent which is used to send Data across the Server and Client, but make sure you secure it, as it can be exploited by ecploiters.

I’m pretty new to RemoteEvents. I tried making a script in ServerScriptService but it didn’t work here:

local remoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local aglstat = player.Stats.AgilityStat

For some reason it thinks player is nil.

You cannot access the Player on the Server, as it contains all the Players, The Client is the only one that will have direct access to it, which can be accessed by using game.Players.LocalPlayer, you can connect the RemoteEvent by saying RemoteEvent.OnServerEvent:Connect(), and giving a function, the first argument should be the Player that fired the Event, which basically happens when the Client fires the Event by saying :FireServer()

2 Likes

Alright, so in the client-side script, I would include this:

remoteEvent:FireServer(2)

And in the server-side script, I’d include this:

function SprintStatIncrease(agl)
	aglstat.Value += agl.Value
end

remoteEvent.OnServerEvent:Connect(SprintStatIncrease)

So the value of agl would be the same as 2, the parameter passed into the event?

Yes, but Remember that the first argument in OnServerEvent will be the Player where the Event was fired from, so from there you are able to access their Data and Apply Changes.

So you will need to add a Second Argument and then apply changes with the given number.

I altered it to look like this:

Client-side

remoteEvent:FireServer(player, 2)

Server-side

function SprintStatIncrease(player, agl)
	aglstat.Value += agl.Value
end

remoteEvent.OnServerEvent:Connect(SprintStatIncrease)

I tried this out and it still thinks “agl” is the same as my player.

Thats because you are now telling the game to send the Player in place of agl when using :FireServer() , so you will need to remove that, The Server will already be aware of what Player it comes from, so there is no need to add another Player argument.

The only time you will need to Add a Player Argument is when using :FireClient(), where you have to Specify which Client to fire to, by giving a Player, or to fire to all Players by using :FireAllClients()

Success! Here’s the final script:

Client-side:

remoteEvent:FireServer(2)

Server-side:

function SprintStatIncrease(player, agl)
	aglstat.Value += agl
end

remoteEvent.OnServerEvent:Connect(SprintStatIncrease)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.