Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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.

0
The remote event was not in replicated storage because I was making a module script, that's why, also thanks Jack_Hase 85 — 4y
Ad

Answer this question