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

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

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)

1 comment

0

Don't forget to press Accept Answer if it helped!

2 answers

✓ Accepted answer

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 Event to 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

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

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