gun = hit.Parent.ClassName("Tool") function Touch(hit) if gun.AmmoType.Value == "9x19mm" then gun.StoredAmmo.Value = gun.StoredAmmo.Value + gun.MaxAmmo.Value end end script.Parent.Touched:connect(Touch)
The script's parent is a part in workspace, what it's supposed to do is find the tool, and if its ammotype is the ammotype that the script is searching for, then it adds ammo, equivalent to the number of MaxAmmo.
However, it did not work. Please help?
The problem is, hit is a nil value until the function on line 2. If you try to do anything with hit before it's defined as a parameter on line 2, you'll get an error.
function Touch(hit) local gun = hit.Parent --Local variables are more efficient. if gun.ClassName ~= "Tool" then return end --return stops everything. Therefore, if hit.Parent is not a tool, nothing beyond this point will execute. if gun.AmmoType.Value == "9x19mm" then gun.StoredAmmo.Value = gun.StoredAmmo.Value + gun.MaxAmmo.Value end end script.Parent.Touched:connect(Touch)