Values don't change with the server after client sets them

  1. What do you want to achieve?
    I am currently trying to make a gun system, which can works based on mags and has different reload stations which can refill the mags. Currently I am using 2 values, one for the current ammo and one for the reserved ammo (mags), mainly so that the server can double check the values and in case of a problem deny the firing of the weapon so that the client cannot exploit infinite ammo.

  2. What is the issue?
    The issue stems from the fact that when the client changes the values, they no longer are updated by the server on the client:
    Here you can see that currently I both have 0 ammo and 0 spare mags
    image
    Then, after coming to a refill station, you can see everything works fine.
    image
    image
    But then after the client changes the values, it doesn’t change them on the client:
    (CLIENT USED UP SOME AMMO)
    image
    image
    And now when we try to refill them after this point, it doesn’t work:
    image
    (PLEASE NOTE: I ADDED A PRINT AFTER AS I NOTICED THE ISSUE WAS NOT CLEAR)

  3. What solutions have you tried so far?
    I tried asking for help on a discord server and searching the forums, but I came up empty handed.

Here’s the refill station code (it’s only a placeholder, don’t bully me lol)

script.Parent.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
	local tool = part.Parent:FindFirstChildOfClass("Tool")
	
	if player and humanoid and tool then
		local reserveAmmo = tool:FindFirstChild("AmmoReserve")
		local mag = tool:FindFirstChild("CurMag")
		local gunStats = tool:FindFirstChild("Settings")
		if reserveAmmo and mag and gunStats then
			print("Refilled!")
			gunStats = require(gunStats)
			reserveAmmo.Value = gunStats.AmmoReserve
			mag.Value = gunStats.AmmoCapacity
		end
	end
end)

Side note: the station is supposed to give you 3 mags, the 2 mags are after reloading

Try using RemoteEvents, remove the ammo by firing the event in your client script and changing it in a Server Script

The problem comes with that, because of ping you could get in a few extra shots from the gun, despite having 0 ammo

Make Variables in your LocalScript that keeps track of the ammo locally and check if there’s enough ammo with the variables

And then here comes the issue with the ammo again, since my main reason for using variables that the server also updates is because of exploits, so in case someone decides to give themselves infinite ammo.
The solution I am mainly looking for is a variable that can be changed on the client, but also if the server changes it the client gets an update too

I am very confused on what your situation is but nevertheless: I believe your best bet would be to hold the ammo on the server and the client with two separate values, rather than one. The local script that would shoot would check if it had enough ammo locally, then would use a RemoteFunction so the server checks if the ammo is what it should be (to prevent exploits) with maybe a 1 ammo threshold to account for ping since that seems to be something else you brought up. Then, the client would take away that ammo from their own value and would then request the server to do the same for the server’s value. Then, when you go to refill ammo, the server would increase its own variable and would then request the client to do the same to its variable.

There isn’t a way to go about this without two variables since, like you said, the server can’t change the client’s.

Hm.
And how would it be better to go about making the values?
Having one “master” server script and separate values for each gun or each gun having it’s own events and values locally?

Overall it’d be best to have one master script with a table.

Sorry for not responding for a while, but I’ll definitely look into this and like you suggested keep separate variables for server and client. I’ll be back once I’ve finish with implementing it and if everything goes smoothly I’ll mark your answer as the solution <3

1 Like

I come to post a small update:
I had a bit of trouble working the previous day so I made the anti-cheat today.
I changed up how my guns handle resupply stations, that being internally by the client and sending a request to the server to reload it. (Currently haven’t made that, only got the ammo working on the server.)

Here’s a quick video showing just that, the pink bullets mark the fact the shot wasn’t cancelled by the server.


Still not sure how the resupply will be able to ask for permission though, since for that I would need to check in on the server if it allows it or not, since it’s based on range and for obvious reasons cough cough ping cough cough the server may decline the resupply despite the player already getting the bullets locally.

Well after a some time I found out I can use remote functions for the permission, so I’ll mark your answer as a solution

Still not sure how the resupply will be able to ask for permission though, since for that I would need to check in on the server if it allows it or not, since it’s based on range and for obvious reasons cough cough ping cough cough the server may decline the resupply despite the player already getting the bullets locally.

I think you’re very much overestimating the effect of ping in that scenario.

Not exactly, in my previous projects ping was a huge problem when looking into stuff like I am doing right now, especially when looking into it as an anti-cheat measure and providing a good accuracy on hits.

sorry don’t know much but can’t you use an attribute?

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