Need help tweaking a raycast gun 2: RemoteFunction Boogaloo

After moving around my gun’s code and using a RemoteFunction to fix a problem mentioned in my previous post (Need help tweaking a raycast gun), I’m getting these errors whenever the RemoteFunction is triggered.

Players.Gibbletplays.Backpack.Pistol2.Script:6: attempt to index nil with ‘TakeDamage’ - Server - Script:6
Players.Gibbletplays.Backpack.Pistol2.Script:6: attempt to index nil with ‘TakeDamage’ - Client - LocalScript:45

LocalScript:

local tool = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage:WaitForChild("OnShoot")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse() 
local shoot_part = tool:WaitForChild("Shoot")

local ammo = tool:GetAttribute("Ammo")
local damage = tool:GetAttribute("Damage")
local cooldown = tool:GetAttribute("cooldown")

tool.Activated:Connect(function()
	if tool.Enabled == true and ammo > 0 then
		blamo(cursor.Hit.Position)
		ammo = ammo - 1
	end
end)

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ReplicatedStorage")

function blamo(position)
	tool.Enabled = false
	tool.Handle.Firesound:Play()
	local origin = shoot_part.Position
	local direction = (position - origin).Unit*300
	local result = Workspace:Raycast(origin, direction)

	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude

	local bullet_clone = ServerStorage.Bullet:Clone()
	bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
	bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
	bullet_clone.Parent = Workspace
	
	local player = tool.Parent.Name
	local part = result.Instance
	local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

	if humanoid then
		player = tool.Parent.Name
		if humanoid.Parent.Name ~= (player) then
			remoteFunction:InvokeServer(result, damage)
		end
	end
	tool.Shoot.flame:Emit(20)
	wait(0.05)
	bullet_clone:Destroy()
	wait(cooldown)
	tool.Enabled = true
end

Script:

local remoteFunction = game.ReplicatedStorage.OnShoot

local tool = script.Parent

local damage = tool:GetAttribute("Damage")

local function Result(player, result, damage)

result:TakeDamage(damage)

print(player, " shot ", result, " and dealt ", damage," damage!")

end

remoteFunction.OnServerInvoke = Result
1 Like

You forgot your 1st argument to Result() is the player.

2 Likes

Now I’m getting a different set of errors.

Players.Gibbletplays.Backpack.Pistol2.Script:6: attempt to index nil with ‘TakeDamage’ - Server - Script:6

Players.Gibbletplays.Backpack.Pistol2.Script:6: attempt to index nil with ‘TakeDamage’ - Server - LocalScript:45

Fixed it. Accidentally called for the “result” variable instead of the “humanoid” variable.

Thanks for helping me out with the first problem!