If statement won't pass Mouse, even though it is clearly retrieved and returned

I’m trying to make a gun script, but I don’t understand why the remote function isn’t returning anything. It gets the mouse and the script continues on, but it won’t pass in the IF function.

local gun = script.Parent
local module = require(game.ServerStorage.GunModule)

local function onActivate()
	wait(0.1)
	local mouse = game.ReplicatedStorage.GetPlayerMouse:InvokeClient(game.Players:GetPlayerFromCharacter(gun.Parent))
	print("Fired? to "..tostring(game.Players:GetPlayerFromCharacter(gun.Parent).Name))
        -- IS fired, proving that the client is invoked
	if mouse then
		print(mouse.Hit.Position)
		-- won't return ANYTHING, extra code unneeded right now
	end
end
gun.Activated:Connect(onActivate)

And if needed, the code for the localscript:

local player = game.Players.LocalPlayer

function game.ReplicatedStorage.GetPlayerMouse.OnClientInvoke()
	local mouse = player:GetMouse()
        -- literally returns the values if printed. very confusing.
	return mouse
end

I honestly cannot figure out why this is happening. If someone could get a fix for this, or a pointer to the right path, this would probably be fixed.

You cannot pass the entire mouse over a remote event or function. The mouse is not replicated. You cannot pass any non-replicated data over a remote

Basically you’d have to send mouse.Hit.p or mouse.Hit by itself

1 Like

Thank you! So I just have to pass the Mouse.Hit value. I’ll update if something goes wrong.

It passed on! But since I don’t want to make another topic, here’s a fun fun bonus issue! :sweat_smile:
I have a gun module that handles raycasting, on the server side.
I get this error:

  22:41:37.254  ServerStorage.GunModule:9: attempt to call a nil value  -  Server - GunModule:9

and my code is this: (line nine marked for convenience reasons:)

module.raycastFromGun = function(gun, posToRaycast)
	local rayParams = RaycastParams.new()
	rayParams.IgnoreWater = true
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.CollisionGroup = "Default"
	rayParams.FilterDescendantsInstances = {gun.Handle}
-- LINE 9 BELOW
	return game.Workspace:Raycast(gun:WaitForChild("Handle").Position,posToRaycast,rayParams)
end

The gun is passed, once again, I don’t see the problem here.

I might be blind, but I don’t see it marked. It’s likely that I’m just stupid though, because I see only three function calls and none of them look nil.

1 Like

haha i didn’t mark it, sorry
I’ll mark it now. Sorry for the inconvenience despite my attempt at convenience.

Well the only thing I think it could be is the WaitForChild, but that should be valid. However because it’s on the server, WaitForChild is completely useless. Especially because you do gun.Handle the line above. I’d just replace gun:WaitForChild("Handle") to just gun.Handle

2 Likes

I did it before, but it returned a different error, with Handle being nil.

If that were the case, you’d get an error on line 8 as well.

1 Like

If you add a print(gun) at the top of the function, what does it print?

1 Like

Screen Shot 2021-07-04 at 10.57.45 PM
WAIT. line “9” is actually line 10. Whoops.

Can you show us where you call the function?

1 Like

That’s rather interesting, how are you calling it? If you call it with a colon : the first argument passed is the module itself.

2 Likes

@JarodOfOrbiter @steven4547466

local function onActivate()
	wait(0.1)
	local mouse = game.ReplicatedStorage.GetPlayerMouse:InvokeClient(game.Players:GetPlayerFromCharacter(gun.Parent))
	print("Fired? to "..tostring(game.Players:GetPlayerFromCharacter(gun.Parent).Name))
	if mouse then
		print(mouse.Position)
-- called here
		local hitPart = module:raycastFromGun(gun,mouse.Position)
		if hitPart then
			if hitPart.Parent:IsA("Model") then
				if hitPart.Parent:FindFirstChildWhichIsA("Humanoid") then
					hitPart.Parent.Humanoid:TakeDamage(10)
				end
			end
		end
	end
end

Try module.raycastFromGun for the reasons I stated above

2 Likes

It passes! I can fix the rest on my own, because I know the rest. Thank you!