Hey guys so I'm coding up a weapons system and for some reason when I fire to the server it doesn't actually run the code in the OnServerEvent. Not sure what is going on, the two events that I am having problems with are:
All Replicated Storage Remote Events that I am trying to fire to the server. Not sure if it's because they are in a module script. It was working before but when I added GunData it broke.
I'm fairly certain I made them correctly.
So I have certain checks and balances to tell if ammo and MaxAmmo stays nill and the print statements that say if they are nil.
I put print statements inside the server script to see if the remote events were being ran and they were not.
Here is the Local Script code that is inside the Pistol Tool:
-- Replicated Storage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerData = ReplicatedStorage:WaitForChild("PlayerData")
local ShootEvent = PlayerData:WaitForChild("ShootEvent")
local ClientShootEvent = PlayerData:WaitForChild("ClientShootEvent")
local AmmoChanged = PlayerData:WaitForChild("AmmoChanged")
local GunData = PlayerData:WaitForChild("GunData")
-- Services
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
-- Player
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
--------------------------------------------------------------------------------------------------------
-- Pistol Data
--------------------------------------------------------------------------------------------------------
-- Pistol Related Variables
local Pistol = script.Parent
local Barrel = script.Parent.Barrel
-- Gun Details
local Ammo = nil
local ClipSize = nil
local MaxAmmo = nil
local AmmoType = nil
local BulletVelocity = nil
local BulletDamage = nil
local FireRate = nil
-- Stoppers
local StopFiring = false
-- Pistol Gui Features
local PistolGui = script.Parent.PistolGui
local TweenReloadTime = nil
local ReloadMessageInvisible = nil
local ReloadMessageVisible = nil
--------------------------------------------------------------------------------------------------------
-- Pistol Functions/RemoteEvents:
--------------------------------------------------------------------------------------------------------
-- Updates Ammo and Pistol Data if data was nil, changed or just equipped
AmmoChanged.OnClientEvent:Connect(function(Ammo2, MaxAmmo2)
Ammo = Ammo2
MaxAmmo = MaxAmmo2
end)
GunData.OnClientEvent:Connect(function(Damage2, FireRate2, ClipSize2, AmmoType2, BulletVelocity2)
BulletDamage = Damage2
FireRate = FireRate2
ClipSize = ClipSize2
AmmoType = AmmoType2
BulletVelocity = BulletVelocity2
PistolGui.PistolFrame.AmmoType.Text = AmmoType
ReloadMessageInvisible = TweenService:Create(PistolGui.PistolFrame.ReloadingMessage, TweenInfo.new(TweenReloadTime), {Transparency = 1})
ReloadMessageVisible = TweenService:Create(PistolGui.PistolFrame.ReloadingMessage, TweenInfo.new(TweenReloadTime), {Transparency = .4})
end)
--------------------------------------------------------------------------------------------------------
-- Equipped and Unequip
--------------------------------------------------------------------------------------------------------
Pistol.Equipped:Connect(function()
-- Retrieve Gun Data
GunData:FireServer("Pistol")
-- Retrieve Ammo From Player
AmmoChanged:FireServer(false, false, Ammo, MaxAmmo)
-- Unanchors weapon if it was anchored.
Pistol.Barrel.Anchored = false
Pistol.Handle.Anchored = false
-- When player Presses R to reload, it calls the Reload Function
ContextActionService:BindAction("Reload", Reload, true, Enum.KeyCode.R)
-- Player Clicks down to fire (Semi Automatic without While loop. So player clicks to fire)
Mouse.Button1Down:Connect(function()
-- Checks to see if Ammo is nil
if Ammo and MaxAmmo then
AmmoChanged:FireServer(false, true, Ammo, MaxAmmo)
else
print("While Firing")
print("Ammo Is Nil: Ammo = ")
print(Ammo)
print("MaxAmmo Is Nil: MaxAmmo = ")
print(MaxAmmo)
AmmoChanged:FireServer(false, false, Ammo, MaxAmmo)
end
end)
end)
Here is the Server Module Script that holds the OnServerEvent:Connect functions:
-- Replicated Storage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerData = ReplicatedStorage:WaitForChild("PlayerData")
-- Services
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
-- Tables
local module = {
Weapons = {
Pistol = {
Damage = 15,
AmmoType = ".45 ACP",
FireRate = .1,
ClipSize = 15,
Velocity = 150
}
}
}
--------------------------------------------------------------------------------------------------------
-- Player Loads In
--------------------------------------------------------------------------------------------------------
-- PlayerAdded
game.Players.PlayerAdded:Connect(function(player)
module.AddPlayerData(player)
end)
-- Adds PlayerData To module
function module.AddPlayerData(player)
module[""..player.UserId] = {
ArmorAmount = 0,
MaxHealthPerArmorSlot = 25,
ArmorSlotsTaken = 0,
Health = 100,
MaxHealth = 100,
Ammo = 0,
MaxAmmo = 30,
}
end
--------------------------------------------------------------------------------------------------------
-- Items and Guns
--------------------------------------------------------------------------------------------------------
-- Remote Events
local AmmoChanged = PlayerData.AmmoChanged
local ShootEvent = PlayerData.ShootEvent
local ClientShootEvent = PlayerData.ClientShootEvent
local GunData = PlayerData.GunData
-- Inserts a Random "Item" into the game where ItemSpawner is: Guns, Armor, Etc..
function module.InsertItemInGame(Item, ItemSpawner)
Item = Item:Clone()
Item.Parent = workspace
Item:MoveTo(ItemSpawner.Position)
ItemSpawner.Parent = game.ServerStorage
end
-- Gun Functions
-- Exploit Checker
local function ExploitChecker(player, Ammo, MaxAmmo)
if Ammo ~= module[""..player.UserId].Ammo or MaxAmmo ~= module[""..player.UserId].MaxAmmo then
player:Kick()
return true
end
end
-- Retrieve Guns Data
GunData.OnServerEvent:Connect(function(player, Gun)
local Weapon = module.Weapons[""..Gun]
local FireRate = Weapon.FireRate
local ClipSize = Weapon.ClipSize
local Damage = Weapon.Damage
local AmmoType = Weapon.AmmoType
local BulletVelocity = Weapon.Velocity
-- Send Gun Data to script (YOU NEED TO MAKE SOMETHING TO CHECK IF IT IS THE RIGHT GUN: GUN ID OR SOMETHING)
GunData:FireClient(player, Damage, FireRate, ClipSize, AmmoType, BulletVelocity)
end)
-- Ammo Safety Check for weapons
AmmoChanged.OnServerEvent:Connect(function(player, Reload, Firing, Ammo, MaxAmmo)
if ExploitChecker(player, Ammo, MaxAmmo)then
return print(""..player.Name" Exploited")
end
if Reload == true then
module.Reload(player)
elseif Firing == false then
local Ammo = module[""..player.UserId].Ammo
local MaxAmmo = module[""..player.UserId].MaxAmmo
AmmoChanged:FireClient(player, Ammo, MaxAmmo)
else
module[""..player.UserId].Ammo = module[""..player.UserId].Ammo - 1
module[""..player.UserId].MaxAmmo = module[""..player.UserId].MaxAmmo
local MaxAmmo = module[""..player.UserId].MaxAmmo
local Ammo = module[""..player.UserId].Ammo
AmmoChanged:FireClient(player, Ammo, MaxAmmo)
end
end)
-- Shot Fired Deal Damage to Player (Add Anti-Exploit)
ShootEvent.OnServerEvent:Connect(function(Player, player, BulletDamage, Ammo, MaxAmmo)
if ExploitChecker(player, Ammo, MaxAmmo)then
return
end
module.DamageDealer(BulletDamage, player)
module[""..player.UserId].Ammo = Ammo - 1
end)
-- Sends Bullet trajectory to all client so they can see bullet
ClientShootEvent.OnServerEvent:Connect(function(Player, BulletData)
ClientShootEvent:FireAllClients(Player, BulletData)
end)
-- Reload Weapon
function module.Reload(player)
local Ammo = module[""..player.UserId].Ammo
local MaxAmmo = module[""..player.UserId].MaxAmmo
-- Does Ammo calculations to make sure it can take ammo from MaxAmmo.
local AmmoGained = 15 - Ammo
if MaxAmmo >= AmmoGained then
Ammo = 15
MaxAmmo = MaxAmmo - AmmoGained
else
Ammo = MaxAmmo
MaxAmmo = 0
end
module[""..player.UserId].Ammo = Ammo
module[""..player.UserId].MaxAmmo = MaxAmmo
AmmoChanged:FireClient(player, Ammo, MaxAmmo)
end
return module
I realize it's alot of code but I'm not getting anything in my output. Everything still runs so not sure what could be causing it to not fire to the server. Didn't include some functions to reduce character count.
Everything Runs perfectly fine other than when it tries to fire to server with those remote events I said. So ammo and maxammo stays nil and it prints out those print statements under else when I try to fire and reload.
ANSWER:
So I didn't realize but I must have copied and pasted local AmmoChanged = PlayerData:WaitForChild("AmmoChanged") in another localscript so it messed it up in this one. As soon as I deleted it everything went back to normal.

6 comments
I didn't include Reload, Most of the mouse1ButtonDown code so if you don't see it that's why
u use the same remote for onclientevent and to fire?
I've done this before, is that really what would stop it?
Did not change the outcome sadly.
Oh try to print when u equip ur tool if it doesnt work u have to change something in the properties :) i think
The print statements in the equip print out already. I just tried it though and it printed out.