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

[Solved] How do I transfer the mouse's CFrame between localscripts and scripts?

Asked by 4 years ago
Edited 4 years ago

I just started programming in Roblox a few days ago, and I'm trying to make a tool that allows the player to teleport to his or her mouse cursor's location. I've got a remote event set up to alert the server that the tool's been used, but I don't know how to transfer the variable that has the mouse's CFrame to the script. Right now, I just have it set up to print the players' name. Could anyone tell me where to go from here?

LocalScript

local tpTool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tpRemote = ReplicatedStorage:WaitForChild("TPRemote")

function getMouseCFrame()
    local mousePosition = mouse.Hit.Position
    print(mousePosition)
    tpRemote:FireServer()

end


tpTool.Activated:Connect(getMouseCFrame) --Running GetMouseCFrame when tool is clicked

Script

local function teleportPlayer(player)
    print(player.Name)
end

game.ReplicatedStorage.TPRemote.OnServerEvent:Connect(teleportPlayer)
0
Don't forget to press Accept Answer if it helped! xXTouchOfFrostXx 125 — 4y

2 answers

Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

In order to transfer Client to Server via Remote Events, you need to specify their Turple Arguments.

What this means is; After specifying that you wish for an Eventto be Fired, you need to state what part you wish to carry over.

So, in the line: tpRemote:FireServer() Between the brackets you must state what you want to be sent over. So in this case, it would be:

tpRemote:FireServer(mousePosition)

As that's what you want to be picked up.

Then, in your ServerScript, you need to require that argument.

game.ReplicatedStorage.TPRemote.OnServerEvent ( player , mousePosition )
-- Your code goes in here
end)

For more information:

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

Ad
Log in to vote
0
Answered by
Nowaha 459 Moderation Voter
4 years ago
Edited 4 years ago

You can add another variable to the OnServerEvent function;

Script:

local function teleportPlayer(player, mousePosition)
    print(player.Name)
    print(mousePosition)
end

game.ReplicatedStorage.TPRemote.OnServerEvent:Connect(teleportPlayer)

Localscript:

function getMouseCFrame()
    local mousePosition = mouse.Hit.Position
    print(mousePosition)
    tpRemote:FireServer(mousePosition) -- Player is passed automatically.

end

Answer this question