Invalid argument #2 (string expected, got nil)

Hello, I have a problem with script.

  1. What do you want to achieve? I want to make it so that when a person shoots a weapon, the type of bullet is selected. I’m trying to make this with Event.

  2. What is the issue? I’m every time getting this error when I testing.

  3. What solutions have you tried so far? I looked in the Developer Hub and found nothing.

Here’s the scripts, that I tried to make.
Server Script:

rs.Remotes.Events:WaitForChild("Shoot").OnServerEvent:Connect(function(player, muzzlePos, mousePos, bulletType)
	if player.Character then
		local sound = player.Character.Torso:FindFirstChild("FireSound")
		if sound then
			sound:Play()
		end
		local bullet = rs.Bullets[bulletType]:Clone() -- error
		bullet.Name = "Bullet"
		bullet.Parent = workspace
		bullet.CanCollide = false
		bullet.Anchored = true
		bullet.CFrame = CFrame.new(muzzlePos, mousePos)
		
		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.Parent = bullet
	end
end)

Local Script:

local framework = {
	inventory = {
		"M4A1";
		"M9";
		"Knife";
		"Frag";
	};
	module = nil;
	viewmodel = nil;
	currentSlot = 1;
}
function shoot()
	if framework.module.fireMode == "Semi" then
		fireAnim:Play()
		framework.module.ammo -= 1
		rs.Remotes.Events:WaitForChild("Shoot"):FireServer(framework.viewmodel.Muzzle.Position, mouse.Hit.p, framework.module.bulletType)
		debounce = true
		wait(framework.module.debounce)
		debounce = false
	elseif framework.module.fireMode == "Full Auto" then
		isShooting = true
	end
end

Module Script:

local Settings = {
	
	canAim = true;
	aimSmooth = .15;
	
	fireAnim = "rbxassetid://11582704656";
	fireSound = game.ReplicatedStorage.Sounds.M4A1.Fire;
	
	bulletType = "Machine";
	
	watchAnim = "rbxassetid://11588898523";
	
	sprintCF = CFrame.new(0, 0, 0) * CFrame.Angles(math.rad(-90/5), math.rad(45/5), math.rad(75/5));
	
	canSemi = true;
	canFullAuto = true;
	
	ammo = 30;
	maxAmmo = 30;
	
	debounce = .81;
	
	reloadTime = 3;
	
	fireMode = "Full Auto";
	
	fireRate = .07;
	
}

return Settings

If someone know solution for this error, then please help me.
Thank you.

Can you be more specific on where your error is?

I marked in the Server Script where the error is.

Have your tried this?

local bullet = bulletType:Clone()

Since the Variable is unknown, the Script or Program marks it as any or a

(Nope, lol)

Yeah, I tried this.
I changed BulletType in ModuleScript to

bulletType = game.ReplicatedStorage.Bullets.Machine;

And now it saying Attempt to index nil with ‘Clone’ error.

I’m not sure if this would work

local bullet = rs.Bullets:FindFirstChild(bulletType):Clone()

Since it uses a string to find the Instance, maybe

It doesn’t work too, now it’s saying Argument 1 missing or nil.

try to replace with

bulletType = framework.module.bulletType
print(bulletType)
rs.Remotes.Events:WaitForChild("Shoot"):FireServer(framework.viewmodel.Muzzle.Position, mouse.Hit.p, bulletType)

what the output saying?

Nothing printing in output. The error stays the same.

no, do same but

bulletType = “Machine”

Yes, I already changed it, but still nothing works.

i see there is another Full Auto code, show it

Try this:

print(rs.Bullets:FindFirstChild(bulletType))

If it find’s the object, it will print its name, otherwise I will print nil which means it can find that object

this one, its your current mode, what happens after?

Ohh, I forgot about it. Now it’s working, thanks for helping!

lol

Text becuause jesiufkesl;fesf

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.