Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Having trouble with ClassNames and Values, please help?

Asked by
Resnex 60
10 years ago
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

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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)
Ad

Answer this question