Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Having trouble with ClassNames and Values, please help?

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?

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.