Module script is not changing variable numbers

Can someone help me with this? I want to make it so that when a player dies they drop a gun and it left the ammo (ex: if player have 2 bullets left out of six when other people pick it up it’ll still be 2)

But this is my script

local DropEvent = script.Parent.DropEvent
local RS = game:GetService("ReplicatedStorage")

DropEvent.OnServerEvent:Connect(function(plr, Action, Bulletsleft, tool) --Bulletsleft in this case is like 2-3-4 or whatever
	if Action == "Used" then
		script.Parent:Destroy()
	elseif Action == "Died" then
		local newtool = tool:Clone()
		newtool.Parent = RS.TempStorage
		local GunModule = require(newtool.GunMod)
		GunModule.CurrentMag = Bulletsleft
		local GunModel = RS.Models.RevolverDrop:Clone()
		GunModel.Tool.Value = newtool
		GunModel.Ammo.Value = Bulletsleft
		GunModel.Parent = plr.Character.Parent
		GunModel:PivotTo(plr.Character.PrimaryPart.CFrame)
	end
end)
local ProximityPrompt = script.Parent
local toolVal = script.Parent.Parent.Tool
local ammoVal = script.Parent.Parent.Ammo
local RS = game:GetService("ReplicatedStorage")

ProximityPrompt.Triggered:Connect(function(player)
	if player.Team.Name ~= "Civilian" then
		return
	end

	local tool = toolVal.Value:Clone()
	tool.Parent = player.Backpack

	local GunModule = require(tool.GunMod)
	GunModule.CurrentMag = ammoVal.Value

	toolVal.Value:Destroy()
	script.Parent.Parent:Destroy()
end)

When I pick it up it has full magazine

You can see I tried storing ammo in an instance as well but still no luck (the instance prints properly and return the actual ‘bullets left’ number)

Any help would be great!

why would you change the module gun states if the gun already has the ammo in the tool, wouldn’t that mean when a player picks it up they already have the ammo that the old player’s gun had?

you shouldn’t change the gun states at all anyway; only the instance values.

Because my gun changes the ammo in local script not serverscript

then there isn’t a way for the ammo to replicate. changing the module wont do anything for the server either.

your best bet is to keep the ammo on the client, but also make the ammo server sided in a way that when you pick up the gun you start with that ammo that the gun originally had

2 Likes

I believe this is the problem here. You cannot change the value of a ModuleScript’s variable via another script, because the change won’t be reflected across the server-client boundary. You can probably use an IntValue object to replicate and store any number changes you wish the server and the clients to be able to see it.

1 Like