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

I need help programming a function I don't know Can someone help?

Anyways, this is an ammo box script, designed mostly for TurboFusion's gun scripts. Anyways, what I need this script to do, is to figure out the name of the tool instead of the classname, so if the gun's named "Double Shotgun" or "Auto Shotgun" the script will only give ammo to those two gun types. Also, there's a glitch where you have 199 ammo, it'll give you 219, which I have no idea how to fix. If someone could help me with the bug, and making it so it'll read the name of the tool, and not the classname, that'd be EXTREMELY helpful!!

Here's the code:

function onTouched(hit) 
if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
local d = hit.Parent:GetChildren() 
for i=1, #d do 
if (d[i].className == "Tool") then 
	if d[i].Ammo.Value == 200 then --This is the max ammo for the gun.
	return nil
	else
d[i].Ammo.Value = d[i].Ammo.Value +20
script.Sound:play()
script.Parent.AmmoLeft.Value = script.Parent.AmmoLeft.Value -0
script.Disabled = true
script.Parent.Transparency = 1
wait(10)
script.Disabled = false
script.Parent.Transparency = 0
end 
end
end
end 
end

script.Parent.Touched:connect(onTouched) 


2 comments

0

I personally don't think you have a clue about Lua scripting. You said you wanted it to recognize a tool by its Name, not className. There is a simple fix on line 5 for that...

0

Actually, I do know Lua scripting. It's just certain things I don't know about.

1 answer

✓ Accepted answer

To remove the glitch, add a "Less Than or Equal To" operator (<=) instead of an "Equal To" operator (==) on line 6. For making it check the name, you can add an or statement on line 5.

function onTouched(hit) 
	if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
	local d = hit.Parent:GetChildren() 
		for i=1, #d do 
			if d[i].Name == "Double Shotgun" or d[i].Name == "Auto Shotgun" then 
				if d[i].Ammo.Value <= 200 then --This is the max ammo for the gun. 
					return nil
				else
					d[i].Ammo.Value = d[i].Ammo.Value +20
					script.Sound:play()
					script.Parent.AmmoLeft.Value = script.Parent.AmmoLeft.Value -0
					script.Disabled = true
					script.Parent.Transparency = 1
					wait(10)
					script.Disabled = false
					script.Parent.Transparency = 0
				end 
			end
		end
	end 
end

script.Parent.Touched:connect(onTouched) 



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