My gun doesn't work

Why doesn’t my code work it just prints nil or baseplate.
It’s a class system and decided to add a shooting mechanic to it.

Module:

-- Mod
local class = {}
class.name = "class"
--Vars
local players = game:GetService("Players")
local player = players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")

-- Classes
class.gunClass = {}
class.swordClass = {}

-- Functions
function class.gunClass.attack(tool, humanoid)
	print("clicked")
	RS:WaitForChild("shot"):FireServer(tool, humanoid)
end

function class.swordClass.attack(tool, humanoid)
	print("clicked")
	RS:WaitForChild("swung"):FireServer(tool, humanoid)
end
-- Return
return class

Client

-- Vars
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local mouse = Player:GetMouse()
local target = mouse.Target
local humanoid = target.Parent:FindFirstChild("Humanoid")
local Character = Player.Character or Player.CharacterAdded:Wait()
local RS = game:GetService('ReplicatedStorage')
local class = require(RS.class)
local tool = Player.Character and Player.Character:FindFirstChildWhichIsA("Tool")
local guns = { 'gun' }
local swords = { 'sword' }

local function checkTool(tool)
	if table.find(guns, tool.Name) then
		tool.Activated:Connect(function()
			class.gunClass.attack(tool, humanoid)
		end)
	elseif table.find(swords, tool.Name) then
		tool.Activated:Connect(function()
			class.swordClass.attack(tool, humanoid)
		end)
	end
end

Player.Backpack.ChildAdded:Connect(function(tool)
	if tool:IsA('Tool') then
		checkTool(tool)
	end
end)

for item, Tool in ipairs(Player.Backpack:GetChildren()) do
	if Tool:IsA('Tool') then
		checkTool(Tool)
	end
end

Server

local RS = game:GetService("ReplicatedStorage")

-- swung
RS:WaitForChild("swung").OnServerEvent:Connect(function(player, tool, humanoid)
	print("swung")
	print(tool)
end)

-- shot
RS:WaitForChild("shot").OnServerEvent:Connect(function(player, tool, humanoid)
	print("shot")
	print(humanoid)
	if humanoid then
		humanoid:TakeDamage(10)
	end
end)

Then I have a tool called “gun” and a rig in the workspace.

1 Like

HELP PLEASE JUST HELP ME :sob:
Hdbcoe djfb

Not anymore!

1 Like

Could you give me a file of an empty workspace with everything in it?
(Also just letting you know, replies can take a few days.)

1 Like

Yeah, Ik impatient… And um I don’t really know how to do that. So if you could tell me that would be nice :sweat_smile:

There is a nowhere in your code that it prints Baseplate or nil, which means you either showing the wrong portion of your code.

You are also not specifying what your code does, which would make it easier to debug.


Edit:

Problem here:

You are not updating the data you used when the weapon was equipped, as a result its obsolete and will just yield the same result.
Whenever the player attacks, you must assign new data to get your expected result.

Not to mention Mouse is obsolete, and you should be Raycasting instead for more accurate, and descriptive results.

3 Likes

File — Save to File as should work.
Screenshot 2023-11-29 183408

1 Like

I’m not using ray casts cuz this is a tech demo, Hit and Target are internally sending ray casts. also, nil just means nothing so when I print the var humanoid and there isn’t one it’s nil. The baseplate part was when I tried directly using Target rather than Humanoid.

You… just contradicted yourself.

Internally yes they are grabbing the data via a raycast, but these are obsolete methods, superceded by a newer raycast system, just how Mouse has been superceded by UserInputService, but thats not the problem here.

The Problem is that you are not updating your code, and as a result, the data remains the same no matter where its fired, you need to fiux this by keeping your Target up to date by assigning a new Target, and then finding the humanoid like so.

-- before all functions, specify your variables

-- lets say the Tool is Activated, and this code fires when it does:
Target = Mouse.Target -- get new target (if Target is specified beforehand, it will be updated)
Humanoid = Target and Target.Parent:FindFirstChild("Humanoid") -- find humanoids

if not Humanoid then return end -- if no humanoid is found, end function.
-- this is to avoid unnessecary calls to the server.
1 Like

for some reason, I don’t have that option.

1 Like

Oops your code didnt load until now nvm

1 Like

I am not sure if this is what you mean cuz I get the error ServerScriptService.globalClassHandler:14: attempt to index nil with ‘Target’ When I use the updated code.

local RS = game:GetService("ReplicatedStorage")

-- swung
RS:WaitForChild("swung").OnServerEvent:Connect(function(player, tool)
	print("swung")
	print(tool)
end)

-- shot
RS:WaitForChild("shot").OnServerEvent:Connect(function(player, tool)
	print("shot")
	print(player)
	local mouse = player:GetMouse()
	local target = mouse.Target
	local hum = target and target.Parent:FindFirstChild("Humanoid")
	if hum then
		hum.Health -= 10
		print(hum.Health)
	end
end)

Client Functions do not exist on the Server (The Server is not in charge a Single Player, A LocalScript is.) , Mouse is only accessible on the Client (aka a LocalScript.)

Update the Data on the Client, and then send the data to the Server, in order to access it properly.

2 Likes

Okie dokie. Thank you very much!

Anyways it doesn’t matter any more cuz I got a solution but thanks!

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