RemoteFunction not returning my projectile

Inside the Server Script

function Throw(plr, args)
	local hit = args[1]
	local hrp = args[2]
	local projectile = game.ServerStorage.Weapons.WeaponProjectiles.ThrowingKnife:Clone()
	local homingForce = projectile.HomingForce
	local projectileInfo = projectile.ProjectileInfo
	projectile.Parent = workspace.Projectiles
	mm.ProjectilePhysics(projectile, hrp.Position, hit, nil, false, (hit - hrp.Position).Magnitude/250)
	local force = (hit - hrp.Position)/((hit - hrp.Position).Magnitude/250) + Vector3.new(0,game.Workspace.Gravity * ((hit - hrp.Position).Magnitude/250) * 0.5, 0)
	local cf = CFrame.new(hrp.Position, hrp.Position + force)
	projectile.CFrame = CFrame.lookAt(cf.Position, cf.Position - cf.UpVector)
	projectile:SetNetworkOwner(plr)
	print(projectile)
	return projectile
end
script.Parent.ThrowFunction.OnServerInvoke = Throw

Inside the Local Script

local projectile = script.ThrowFunction:InvokeServer({hit, hrp})
print(projectile)

I’m trying to have the server script pass the projectile it created to the local script so that it can handle the hit detection. But for whatever reason it doesn’t pass it. On the print statement in the server script it will print fine, but doesnt return the projectile to the Local Script. If I make the Server Script return anything else like a string or the baseplate, it will work fine though. Any idea why this is happening?

1 Like

it may need some time for the cloned projectile to be replicated to the client,
when the invokeserver returns, it may not yet see the replicated projectile clone so it prints nil

I tried making it wait 1 second before it prints, still didnt return anything.

1 Like

Can you try waiting more than 1 seconds just to test if it’s infact not a replication issue.

I tested with task.wait(1), and it works

but regardless, i think it is unstable, because it depends on whether the projectile is replicated, and you probably don’t want excessive delay in getting the projectile back

i had it wait 3 seconds, still nothing.

So it prints projectile on the server but on the client it printd nil is that the problem currently?

1 Like

Here’s the full local script in case it helps

local soundScript = script.ThrowingKnifeSoundScript
local plr = game.Players.LocalPlayer
local UIP = game:GetService("UserInputService")
local mouse = plr:GetMouse()
local throwCD = .2
local currentCD = 0
local char
local mouseDown = false
repeat
	char = plr.Character
	task.wait(.1)
until char ~= nil 
local hrp = char:WaitForChild("HumanoidRootPart")
local info = char.Info
local toolInfo = info.ToolInfo
local statFolder = info.Stats
local mm = require(workspace.MainModule)
mouse.Button1Down:Connect(function()
	mouseDown = true
end)
mouse.Button1Up:Connect(function()
	mouseDown = false
end)
repeat
	if mouseDown == true and currentCD <= 0 and toolInfo.Equipped.Value == "Secondary" and toolInfo.Secondary.Value == "ThrowingKnife" then
		currentCD = throwCD
		local hit 
		if mouse.Target ~= nil and (mouse.Hit.Position - hrp.Position).Magnitude <= 150 then
			hit = mouse.Hit.Position
		else
			hit = hrp.Position + CFrame.new(hrp.Position, mouse.Hit.Position).LookVector * 150
		end
		local projectile = script.ThrowFunction:InvokeServer({hit, hrp})
		task.wait(3)
		print(projectile)
	end
	task.wait()
	currentCD = math.clamp(currentCD - .015, 0, math.huge)
until char.Humanoid.Health <= 0

server side

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage.RemoteFunction.OnServerInvoke = function()
	local projectile = ServerStorage.TestProjectile:Clone()
	projectile.Parent = workspace.Projectiles
	task.wait(1)
	return projectile
end

client side

game:GetService("UserInputService").InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local projectile = game:GetService("ReplicatedStorage").RemoteFunction:InvokeServer()
		print(projectile)
	end
end)

thats weird, im not sure why it works for you and not me

No as in the wait put on the server before returning to test not on the client

Okay its returning now, thank you

1 Like

I would suggest to make use of the ChildAdded event to get the projectile as soon as it is replicated

Server side

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player: Player)
	local projectile = ServerStorage.TestProjectile:Clone()
	projectile:SetAttribute("UserId", player.UserId)
	projectile.Parent = workspace.Projectiles
end)

client side

local LocalPlayer = game:GetService("Players").LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		game:GetService("ReplicatedStorage").RemoteEvent:FireServer()
	end
end)

local projectilesFolder = workspace:WaitForChild("Projectiles")
projectilesFolder.ChildAdded:Connect(function(child: Instance)
	if child:GetAttribute("UserId") == LocalPlayer.UserId then
		print("I fired this", child)
	end
end)