Transfer variable value from localscript to a normal script

Im trying to make a block spawn at the mouse position when the player clicks, but for that i need to specify where the mouse position is.

Localscript:

local replicatedstorage = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
replicatedstorage.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousepos = player:GetMouse().hit.Position		
game.ReplicatedStorage.CubeSpawn:FireServer()
end end)

Normal script:

script.Parent.OnServerEvent:Connect()
local Newpart = Instance.new("Part")
Newpart.Size = Vector3.new(10, 10 ,10)
Newpart.Position = Vector3.new(mousepos)

I know that i need to pass the value to the script but i have no idea how i can do that.

1 Like

Also i forgot to mention that the normal scripts parent is a remoteevent called CubeSpawn
I dont know how to pass values with a remote event

You would just pass it through the parameters:

local replicatedstorage = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

replicatedstorage.InputBegan:Connect(function(input, gameProcessedEvent)
   if input.UserInputType == Enum.UserInputType.MouseButton1 then
       local mousepos = player:GetMouse().hit.Position		
       game.ReplicatedStorage.CubeSpawn:FireServer(mousepos) -- send it through here
    end 
end)
--                                          The player who fired the remote event is ALWAYS the first argument on the server (don't pass the player from the client)
script.Parent.OnServerEvent:Connect(function(player, mousepos)
    local Newpart = Instance.new("Part")
    Newpart.Size = Vector3.new(10, 10 ,10)
    Newpart.Position = mousepos -- mousepos is already a Vector3, so just use the variable
    Newpart.Parent = workspace -- Parent the part to workspace so it's visible
end)

Also, judging by the fact that you detect the remote with script.Parent, I assume the server script is in ReplicatedStorage, and if thats the case, move it to ServerScriptService

1 Like

You just put your variables inside of the paramteres,

remote:Fire(var1, var2, var3)

and on the server you use the OnServerEvent event to detect whenever it gets fired. It will also return all the variables you put inside the parameters.

remote.OnServerEvent:Connect(function(player, var1, var2, var3)
– code
end)

remember that the first variable is always the player who fired the remote.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.