Player.Character and Module scripts not working

This is going to drive me crazy, Ive done everything I can to test the Framework - Client local script so im going to put it out there and ask if someone can help me

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local aimCF = CFrame.new()

local mouse = player:GetMouse()

local isAiming = false
local isShooting = false
local isReloading = false
local canShoot = true

local debounce = false

local currentSwayAMT = 0
local swayAMT = 0.6
local aimSwayAMT = -0.2
local swayCF = CFrame.new()
local lastCameraCF = CFrame.new()

local currentSlot = 1 -- starting slot
local lastSlot = 0
local maxSlots = 3

local fireAnim = nil

local particle
local flash
local bigFlash
local timer
local bobOffset = CFrame.new()

local framework = {
	inventory = {
		"Rocket Launcher";
		"Paintball Gun";
		"Sword";
	};
	
	module = nil;
	viewmodel = nil;
	currentSlot = 1;
}



function loadSlot(Item)
	local viewmodelFolder = game.ReplicatedStorage.ViewModels
	local moduleFolder = game.ReplicatedStorage:WaitForChild("Modules")

	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end

	if moduleFolder then
		framework.module = moduleFolder:WaitForChild(Item)
		if viewmodelFolder:FindFirstChild(Item) then
			framework.viewmodel = viewmodelFolder:FindFirstChild(Item):Clone()
			framework.viewmodel.Parent = camera
		end
		print(framework.module.ammo)
		if framework.viewmodel and framework.module and character then
			-- animation
			fireAnim = Instance.new("Animation")
			fireAnim.Parent = framework.viewmodel
			fireAnim.Name = "Fire"
			fireAnim.AnimationId = framework.module.fireAnim
			fireAnim = framework.viewmodel.AnimationController.Animator:LoadAnimation(fireAnim)
			-- sound
			game.ReplicatedStorage.Events.LoadSlot:FireServer(framework.module.fireSound)
			flash = framework.viewmodel.HumanoidRootPart.Attachment.flash
			bigFlash = framework.viewmodel.HumanoidRootPart.Attachment.bigFlash
		end
	end
end

local hud = player.PlayerGui:WaitForChild("GameUI")

RunService.RenderStepped:Connect(function()
	local rot = camera.CFrame:ToObjectSpace(lastCameraCF)
	local X, Y, Z = rot:ToOrientation()
	swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * currentSwayAMT, math.sin(Y) * currentSwayAMT, 0), .1)
	lastCameraCF = camera.CFrame

	if framework.viewmodel and framework.module then
		hud.gun.Icon.id.Text = framework.inventory[framework.currentSlot]
		--hud.ammo.amount.Text = framework.module.ammo .. "/" .. framework.module.maxAmmo
	end

	if humanoid then
		local movementOffset = CFrame.new()

		if framework.viewmodel ~= nil and framework.module ~= nil then
			if humanoid.MoveDirection.Magnitude > 0 and humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
				if isAiming == true then
					bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 4) * .05, -humanoid.CameraOffset.Y/3, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05), .005)
				else
					bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 4) * .05, -humanoid.CameraOffset.Y/3, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05), humanoid.WalkSpeed/45)
				end
			end
		else
			bobOffset = bobOffset:Lerp(CFrame.new(0, -humanoid.CameraOffset.Y/3, 0), .1)
		end

	end

	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v.PrimaryPart.CFrame = (camera.CFrame * swayCF * aimCF * bobOffset)
			--updateCameraShake()
		end
	end

	if isAiming and framework.viewmodel ~= nil and framework.module.canAim then
		local offset = framework.viewmodel.AimCamera.CFrame:ToObjectSpace(framework.viewmodel.PrimaryPart.CFrame)
		aimCF = aimCF:Lerp(offset, framework.module.aimSmooth)
		currentSwayAMT = aimSwayAMT
	else
		local offset = CFrame.new()
		aimCF = aimCF:Lerp(offset, .2)
		currentSwayAMT = swayAMT
	end
	
	-- HUD
	if hud and moduleFolder then
		if framework.module.ammo <= framework.module.maxAmmo/4 then
			hud.ammo.Icon.ImageColor3 = Color3.fromRGB(255, 0, 0) 
		else
			hud.ammo.Icon.ImageColor3 = Color3.new(1, 1, 1)
		end
		hud.gun.Icon.ImageId = framework.module.icon
		if isReloading then
			hud.ammo.Icon.reload.Visible = true
			hud.ammo.Icon.ImageTransparency = 0.75
		else
			hud.ammo.Icon.reload.Visible = false
			hud.ammo.Icon.ImageTransparency = 0
		end
	end
end)

local function updateWeapon(slot)
	if isReloading == true and canShoot == false and framework.module.ammo ~= 0 then
		isReloading = false
		canShoot = true
	end
	if isReloading == false then
		framework.currentSlot = slot
		loadSlot(framework.inventory[currentSlot])
	end
end

updateWeapon(1)


-- SLOT CHANGING
local function changeSlot(howMuch)
	currentSlot += howMuch
	if currentSlot > maxSlots then
		currentSlot = 1
	elseif currentSlot < 1 then
		currentSlot = maxSlots
	end
	updateWeapon(currentSlot)
end

local function setSlot(whatSlot)
	if framework.currentSlot ~= whatSlot then
		currentSlot = whatSlot
	end
	updateWeapon(currentSlot)
end

local function reload()
	if isReloading == false and framework.module.maxAmmo ~= 0 and framework.module.ammo ~= framework.module.maxAmmo then
		canShoot = false
		isReloading = true
		
		wait(framework.module.reloadTime)
		
		if isReloading == true then
			framework.module.ammo = framework.module.maxAmmo
		end
		canShoot = true
		isReloading = false
		
	end
end

-- SHOOTING --
local function fireWeapon()
	if character and framework.viewmodel and framework.module and framework.module.ammo ~= 0 and debounce == false and isReloading == false	and canShoot == true and framework.module.maxAmmo ~= 0 then
		game.ReplicatedStorage.Events.Shoot:FireServer()
		fireAnim:Play()
		character.Torso.FireSound:Play()
		framework.module.ammo -= 1
		debounce = true
		
		wait(framework.module.fireRate)
		
		debounce = false
	elseif character and framework.viewmodel and framework.module and debounce == false and framework.module.maxAmmo == 0 then
		game.ReplicatedStorage.Events.Shoot:FireServer()
		fireAnim:Play()
		character.Torso.FireSound:Play()
		debounce = true

		wait(framework.module.fireRate)

		debounce = false
	end
end

local function trigger()
	if isReloading == true and canShoot == false and framework.module.ammo ~= 0 then
		isReloading = false
		canShoot = true
	end
	
	if framework.module.fireMode == "Semi" then
		fireWeapon()
	end

	if framework.module.fireMode == "Full Auto" then
		isShooting = true
	end
end

local oldCamCF = CFrame.new()

function updateCameraShake()
	local newCamCF = framework.viewmodel.FakeCamera.CFrame:ToObjectSpace(framework.viewmodel.PrimaryPart.CFrame)
	camera.CFrame = camera.CFrame * newCamCF:ToObjectSpace(oldCamCF)
	oldCamCF = newCamCF
end


-- INPUTS -- 

UserInputService.MouseIconEnabled = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		setSlot(1)
	end
	
	if input.KeyCode == Enum.KeyCode.Two then
		setSlot(2)
	end
	
	if input.KeyCode == Enum.KeyCode.Three then
		setSlot(3)
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL1 then
			changeSlot(1)
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR1 then
			changeSlot(-1)
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton3 then
		print("middleButton")
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = true
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if character and framework.viewmodel and framework.module then
			trigger()
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL2 then
			isAiming = true
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR2 then
			trigger()
		end
	end
	
	-- RELOAD
	if input.KeyCode == Enum.KeyCode.R then
		reload()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = false
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL2 then
			isAiming = false
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		isShooting = false
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR2 then
			--endFire()
		end
	end
end)

mouse.WheelForward:Connect(function()
	changeSlot(1)
end)

mouse.WheelBackward:Connect(function()
	changeSlot(-1)
end)

--game.ReplicatedStorage.Events.PlayerAdded.OnClientEvent:Connect(function(ply, char)
	
	--player = game.Players.LocalPlayer
	
	--character = player.Character
	
	--humanoid = character:WaitForChild("Humanoid")

--	humanoid.Died:Connect(function()
--		local isAiming = false
--		local isShooting = false
--		local isReloading = false
--		local canShoot = true

--		local debounce = false

--		local currentSwayAMT = 0
--		local swayAMT = 0.6
--		local aimSwayAMT = -0.2
--		local swayCF = CFrame.new()
--		local lastCameraCF = CFrame.new()

--		local currentSlot = 1 -- starting slot
--		local lastSlot = 0
--		local maxSlots = 3

--		local fireAnim = nil

--		local particle
--		local flash
--		local bigFlash
--		local timer
--		local bobOffset = CFrame.new()

--		local framework = {
--			inventory = {
--				"Rocket Launcher";
--				"Paintball Gun";
--				"Sword";
--			};

--			module = nil;
--			viewmodel = nil;
--			currentSlot = 1;
--		}
--
--		--loadSlot(framework.inventory[currentSlot])
--	end)
--end)  


while wait() do
	if isShooting and framework.module.ammo > 0 and isReloading ~= true and canShoot == true then
		framework.module.ammo -= 1
		fireAnim:Play()
		game.ReplicatedStorage.Events.Shoot:FireServer()
		mouse.Button1Up:Connect(function()
			isShooting = false
		end)
		
		wait(framework.module.fireRate)
	end
end

Framework Testing.rbxl (125.5 KB)

If anyone fixes the error please let me know :pray:

edit (I forgot to add what the problem was lol)

Problems:

  • Line 1 of the given code above local player = game.Players.LocalPlayer
  • There is an error in the output saying ammo is not a valid member of ModuleScript "ReplicatedStorage.Modules.Rocket Launcher" - Client - Framework - Client:70

I got to the next error by requiring the module, this is near the top of the local script

function loadSlot(Item)
	local viewmodelFolder = game.ReplicatedStorage.ViewModels
	local moduleFolder = game.ReplicatedStorage:WaitForChild("Modules")

	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end

	if moduleFolder then
		framework.module = require(moduleFolder:WaitForChild(Item))

The next error is Torso is not a valid member of my characters model

1 Like

I forgot to set the player to be in R6 before downloading the game file. I will have an edit with the updated game now, Thanks!

1 Like

Make sure you update the client with this:

function GetTorso(Character)
	return Character:FindFirstChild("Torso") or Character:FindFirstChild("LowerTorso")
end

-- SHOOTING --
local function fireWeapon()
	if character and framework.viewmodel and framework.module and framework.module.ammo ~= 0 and debounce == false and isReloading == false	and canShoot == true and framework.module.maxAmmo ~= 0 then
		game.ReplicatedStorage.Events.Shoot:FireServer()
		fireAnim:Play()
		GetTorso(character).FireSound:Play()
		framework.module.ammo -= 1
		debounce = true
		
		wait(framework.module.fireRate)
		
		debounce = false
	elseif character and framework.viewmodel and framework.module and debounce == false and framework.module.maxAmmo == 0 then
		game.ReplicatedStorage.Events.Shoot:FireServer()
		fireAnim:Play()
		GetTorso(character).FireSound:Play()
		debounce = true

		wait(framework.module.fireRate)

		debounce = false
	end
end

and the server with this:

function GetTorso(Character)
	return Character:FindFirstChild("Torso") or Character:FindFirstChild("LowerTorso")
end

game.ReplicatedStorage.Events.LoadSlot.OnServerEvent:Connect(function(player, Sound)
	if player.Character	then
		local Torso = GetTorso(player.Character)
		for i,v in pairs(Torso:GetChildren()) do
			if v.Name == "FireSound" then
				v:Destroy()
			end
		end
		
		local fireSound = Sound:Clone()
		fireSound.Parent = Torso
	end
end)

game.ReplicatedStorage.Events.Shoot.OnServerEvent:Connect(function(player)
	if player.Character then
		local Torso = GetTorso(player.Character)
		local sound = Torso:WaitForChild("FireSound")
		if sound then
			sound:Play()
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		game.ReplicatedStorage.Events.PlayerAdded:FireClient(player, character)
	end)
end)
1 Like

Oh nice that works then! dont need to add my torso changes

Thank you! This worked perfect after I replaced the GetTorso() functions with character.Torso

1 Like

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