How to make a local event send information from a value that's been changed on local side to server side

I’m not really sure how to explain it but I’m trying to send information like script.Parent.Values.CurrentAmmo.Value (or the Value of CurrentAmmo, Values is the folder name), when I’m doing something like CurrentAmmoEvent:FireServer(). I then want to be able to take the information fired from the local event to the server, and be able to use it in the server script that accepts the local event. If all of this makes sense, please help.

1 Like

You can input information into FireServer as an argument for the server to receive information from the client. Make sure you are sending in readable inputs and not objects as those are considered nonreadable. Similar to how functions have parameters, OnServerEvent has (player parameter, any other parameter).

What I understood is: You want local changes to be reflected on the server? (e.g. the player’s ammo changed in the client, and you want that change in ammo to be sent to the server?)

I highly discourage this practice, as exploiters may change any value in the client. Operations like changing ammo, killing players, ending the round, etc. should be handled by the server only.

If one exploiter were to change the value of their ammo to be, let’s say, 9999999, and that is sent to the server, now the player has an OP amount of ammo.

1 Like

Guess you’re right, but my entire gun system is built on the local side with server inputs only handling stuff like bullet casings and sounds, so what’s another bullet to the body? I want to do what your first statement says

How would I input information into FireServer? Do you mean something like this? CurrentAmmoEvent:FireServer(script.Parent.Values.CurrentAmmo.Value)

2 Likes

Ammo can be stored in the server, and functions that shoot bullets and reduce ammo, reload ammo, and detect bullet hits can be done in the server, with the client relying on RemoteEvent:FireServer().

The RemoteEvents that will be used should be used like an impulse generator, rather than a sender (or in dialog form: RemoveEvents should say “Hey, server, please do this action for me” rather than “Hey, server, this is how many bullets I have”). The server, however, can send values from the server to the client.

An example of a safer gun system (The following code is not exactly usable, nor possibly functional. The example code should give an idea as to how to use Remotes properly):

-- Server (Script, RunContext: Server)

-- Values in the server script, controlled by the server script.
local ammo = 30
local ammoCap = 30
local currentUser = nil

local isGunEquipped = false
local isReloading = false

local reloadMagazineEvent = script.Parent:FindFirstChild("Reload")
local shootBulletEvent = script.Parent:FindFirstChild("Shoot")

shootBulletEvent.OnServerEvent:Connect(function(Player : Player)
	if currentUser == nil or not isGunEquipped then return end
	if currentUser ~= Player.Character then return end 
	if isReloading or ammo <= 0 then return end
	
	gunData.ammo -= 1
end)

reloadGunEvent.OnServerEvent:Connect(function(Player : Player)
	if currentUser == nil or not isGunEquipped then return end
	if currentUser ~= Player.Character then return end 
	if isReloading or ammo >= ammoCap then return end
	
	isReloading = true
	task.wait(7)
	ammo = ammoCap
	isReloading = false
end)

script.Parent.Equipped:Connect(function()
	currentUser = script:FindFirstAncestorWhichIs("Model")
	isGunEquipped = true
end)
script.Parent.Unequipped:Connect(function()
	currentUser = nil
	isGunEquipped = false
end)
-- Client (Script, RunContext: Client)
local reloadMagazineEvent = script.Parent:FindFirstChild("Reload")
local shootBulletEvent = script.Parent:FindFirstChild("Shoot")

script.Parent.Activated:Connect(function() -- Assume the parent is a Tool
	shootBulletEvent:FireServer() -- Impulse
end)

-- Reloadig gets a little bit complicated.
-- Pressing R should call `reloadMagazineEvent:FireServer()`

I tested none of the code, but again, this should help you give an idea of how to use Remotes correctly.

Even the hit detection? this is such a wrong way of making a gun system, the only thing the client should do is send its inputs

1 Like

Yeah, this sounds like a disaster for you and a party for exploiters. It should be the exact opposite. Never trust the client. Handle all of this on the server, and then inform the client of what’s happening.

1 Like