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.
It passed on! But since I don’t want to make another topic, here’s a fun fun bonus issue!
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.
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
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