Remote Function not invoking... or returning value?

What the script should do is invoke the client, the client of which then returns the mouse.Hit.P value for use in setting the BodyGyro’s CFrame. However, it does not invoke (or return?) and there are no errors in the output.

Here is the explorer, server script, local script and output:

Local:

local p = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local MoPOS = p:WaitForChild("MoPOS")

MoPOS.OnClientInvoke = function()
    return mouse.Hit
end

Server:

local p = script.Parent
local plr
local mousePos
p.Parent.Parent.Player.Changed:Connect(function()
	plr = p.Parent.Parent.Player.Value
	print (plr)
end)

p.Active.Changed:Connect(function()
	local function lookAtMouse ()
		print "function began"
		if p.Parent.Parent.Player.Value == nil then return else end
		print "passed return test"
		mousePos = p.MoPOS:InvokeClient(plr)
		print "invoked"
		--local direction = (mousePos - p.Core.Position).unit
		p.Cannon.BodyGyro.CFrame = CFrame.new(p.Cannon.Position, mousePos)
		print "gyro set"
	end
	
	while wait (0.1) do
		print "beginning function"
		lookAtMouse()
		print "epic it workd"
	end
end)

Screenshot (115) Screenshot (116)
I have no idea why it isn’t working.

1 Like
MoPOS.OnClientInvoke = function() 
     return mouse.Hit 
end

You are actually returning the CFrame that the mouse is pointing at, and not the Position. This should fix it.

MoPOS.OnClientInvoke = function()
    return mouse.Hit.p
end

Wouldn’t it be Target if it was returning the object that the mouse is pointing at then? Hit is a CFrame, Position (p is deprecated) is a Vector3 (see: CFrame.Position).

Oh, right. Sorry about that! I can be quite forgetful sometimes.

EDIT: I’ve fixed it now

I’ve just tested this out, however the problem still persists

plr = p.Parent.Parent.Player.Value 
print (plr)
mousePos = p.MoPOS:InvokeClient(plr)

Ah, I see the problem now. You are attempting to invoke a string, which is the plr name, instead of the plr object. This should fix it.

plr = game:GetService("Players")[p.Parent.Parent.Player.Value]
mousePos = p.MoPOS:InvokeClient(plr)

I tried it out and got this error :
tyh

Edit 1 : I also tested out a remote event to see if my previous script would fire onto the client, which it did not. So this could be a problem with the player.

Edit 2 : I set the objectvalue (the p.Parent.Parent.Player) with the returned value of a clickdetector but the problem remains.

Edit 3 : I set up the script to kill the character by going through the player’s properties, so the player isn’t the issue.

Sorry about that, please remove the print(plr) and it should work

The error is there with and without the print(plr)

Could you show me a updated version of your script (like the code is in your script rn)?

your printing is kinda weird, instead of:

print "sdkfhasudfh"

do:

print("jkafsakfasdli")

There is no problem or difference (in functionality) with doing print("stuf") and print "stuf"

local p = script.Parent
local plr
local mousePos

p.Parent.Parent.Player.Changed:Connect(function()
	plr = game:GetService("Players")[p.Parent.Parent.Player.Value]
	--print (plr)
end)

p.Parent.Activate.ClickDetector.MouseClick:Connect(function(clicked)
	plr = clicked
	--print (plr)
	p.Active.Value = true
	local function lookAtMouse ()
		print "function began"
		mousePos = p.MoPOS:InvokeClient(plr)
		print "invoked"
		--local direction = (mousePos - p.Core.Position).unit
		p.Cannon.BodyGyro.CFrame = CFrame.new(p.Cannon.Position, mousePos)
		print "gyro set"
		wait (0.5)
	end
	while true do
		print "work"
		lookAtMouse()
		print "worked!!!"
	end
end)

I replaced the Changed event detection with the clickdetector button, and replaced the part with your code as well as some other bits and bobs

The localscript is the same.

Any errors or something that printed in the output?

Only the one i showed before
tyh

There might be a chance this is located in the workspace and local scripts cant be run in workspace, rather move it into starter pack

1 Like

Oh, try replacing

plr = game:GetService("Players")[p.Parent.Parent.Player.Value]

with

plr = game:GetService("Players"):FindFirstChild(p.Parent.Parent.Player.Value)

That solved it and the cannon now looks at the mouse, thank you!

People of the future & google searchers - behold, your solution! If it doesnt work then i lol @ u