I am creating a gun system and I ran into the issue of the client not updating its values for the ammo.
Sorry if this is a noob question, just started a week ago.
Here’s how the code is supposed to work:
1: Both Client and Server have access to the same modulescript that has the [“ammo”] set to 31.
2: Client on mouse click checks if [“ammo”] < 1. If it is, return the function, thus not letting it fire.
3: Server also checks ammo by if [“ammo”] > 0 then execute fire(function)
4: Client fires a “fake” bullet preemptively on client side to compensate for the delay between client to server
5: Server fires an invisible bullet by firing a raycast a couple milliseconds later and replicating it to all other clients
6: Every time the server successfully fires a bullet it will do stat[“ammo”] = stat[“ammo”] - 1
7: Both the client and server should stop firing bullets as soon as the “ammo” value from the modulescript is below < 1
What happens is that the server works as intended; however, the client is still stuck at “31”, the value it was set to by default.
I know this because I made both the server and client print the ammo value every shot.
Server is at 0, resulting in the script rejecting calls to fire the bullet. Client is stuck at 31, causing it to still fire client side bullets.
LocalScript:
local stats = require(game:GetService("ReplicatedStorage").WeaponS)
local function fire()
if not CanFire or not firee or stats["ammo"] < 1 or reloading then
return
end
MouseEvent:FireServer(FireDirection)
print(stats["ammo"])
end
Server Script:
local function onFire()
if stats["ammo"] > 0 then
shoot()
stats["ammo"] = stats["ammo"] - 1
print(stats["ammo"])
end
end
If the module is kept in a server-only location, the client cannot see it, but if it’s kept in a location that both the server and client can see, it’s exploitable. This is probably the best method.
The clientscript, serverscript and the modulescript are all children of the tool.
How do you exploit a table and a serverscript that has pre-inputted values?
They only listen for mouse clicks and direction and do their own checks.
You can see the localscript returning “31”, while the serverscript and another serverscript that checks the same module, both return “0”.
Is this a bug then? Or do localscripts just not stay updated on variable changes in the modulescript?
Just some additional info:
As you observed, when you require a module from both the client and the server, it is going to run the module separately for each.
Another option for keeping a value up to date on the client is to put a “NumberValue” instance (or “StringValue” or whatever) into ReplicatedStorage and changing that from the server. Then, you can hook up a Changed event on the value from the client to stay up to date.