Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Passing variables between scripts with a remote event makes variables nil?

So I was fixing an old script that used to be for no FE, I ran into this problem (This is near a TP script) Script :

	script.MousePos.OnServerEvent:Connect(function(plr, SPOT)
		print(SPOT)
	end)

LocalScript:

script.Parent['Weapon GUI'].a.MousePos.OnClientEvent:Connect(function()

local mouse = game.Players.LocalPlayer:GetMouse()
local camPos = game.Workspace.CurrentCamera.CoordinateFrame.p

local mous =  mouse.hit.p
script.Parent['Weapon GUI'].a.MousePos:FireServer(plr, Vector3.new(mous))
print(mous)
end)

now when I fire the client via the remote event to get mouse vector3 value, the local script works, prints mous correctly, but when it fires the server, mous is nil, I have tried without the Vector3.new(mous), still didn't work, also the error basically said it expected a vector3 value but it got nil.

1 answer

✓ Accepted answer

This code shouldn't error, it should just be printing your name since you passed a reference to your local player to the server.

It is unnecessary to pass a reference to the player who fired the remote event. This is done implicitly by roblox. It wouldn't make sense for it to need to be done manually, as it could be a security issue, an exploiter could fire on behalf of another player.

Vector3.new(mous) should just evalúate to a vector in the origin (0, 0, 0) since the constructor function was given improper arguments. If no arguments or improper arguments are given, it just returns a vector at (0, 0, 0). mous is already a Vector3, so why try constructing a Vector3 out of another?

Next, indentation and the variable names. Indent each time you're in a function block, a do end block, or in a control structure block.

mous is more appropriate for a variable pointing to the mouse instance, but you already do that with mouse. position seems pretty reasonable.

Oh and mouse is a constant so it doesn't need to be declared every time the event is fired.

Finally, Mouse.hit, Camera.CoordinateFrame and CFrame.p are deprecated in favour of Mouse.Hit, Camera.CFrame and CFrame.Position, respectively.

Final product

local weaponUI = script.Parent["Weapon UI"]
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local camera = game:GetService("Workspace").CurrentCamera

weaponUI.a.MousePos.OnClientEvent:Connect(function()
    local cameraPosition = camera.CFrame.Position
    local mousePosition =  mouse.Hit.Position
    weaponUI.a.MousePos:FireServer(mousePosition)
end)

Not sure why you have the remote event in a GUI, because then the server won't be able to see it. Remotes should always be on replicated storage unless you have a specific case where it needs to be elsewhere, like remotes for guns, for example.

1 comment

0

The remote event was not in replicated storage because I was making a module script, that's why, also thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.