#Passing Parameters Via FireServer

16 messages · Page 1 of 1 (latest)

cold jungle
#

I would like to pass a variable (positionX) from a localscript to a script but am not sure how to accomplish that

#

and here are my scripts:

#

localscript:

local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ghostBrick = game.ReplicatedStorage.GhostBrick:Clone(); ghostBrick.Parent = game.Workspace 
local placeBrick = replicatedStorage:FindFirstChild("PlaceBrick")

local gridSize = 2.2

local function snap(initialValue)
    local roundedValue = math.floor(initialValue/gridSize)*gridSize
    
    return roundedValue
end

local function move()
    local mouseX = mouse.Hit.Position.X
    
    if mouseX > -48.4 and mouseX < 50.4 then
        ghostBrick.Position = Vector3.new(snap(mouse.Hit.Position.X), 4.15, 0)
    end
end

local function onMouseClick()
    local positionX = ghostBrick.Position.X
    
    placeBrick:FireServer(positionX)
    print("fired to server")
end

runService.RenderStepped:Connect(move)
mouse.Button1Up:Connect(onMouseClick)
#

script:

local replicatedStorage = game:GetService("ReplicatedStorage")

local brick = replicatedStorage.Brick
local placeBrick = replicatedStorage:FindFirstChild("PlaceBrick")

local function newBrick()
    local cloneBrick = brick:Clone(); brick.Parent = game.Workspace; brick.Position = Vector3.new(0, 4.15, 0)
end

placeBrick.OnServerEvent:Connect(newBrick)
#

oh and I am trying to use positionX for the x position of cloneBrick

old lotus
#
local replicatedStorage = game:GetService("ReplicatedStorage")

local brick = replicatedStorage.Brick
local placeBrick = replicatedStorage:FindFirstChild("PlaceBrick")

local function newBrick(player, positionX)
    local cloneBrick = brick:Clone(); brick.Parent = game.Workspace; brick.Position = Vector3.new(0, 4.15, 0)
end

placeBrick.OnServerEvent:Connect(newBrick)
#

.OnServerEvent passes the player parameter as its first no matter what, to index the player which fired the event, then starts the parameters you sent

#

@cold jungle

cold jungle
#

@old lotus thanks

#

I'll try this

old lotus
cold jungle
#

I'll let you know if that worked :)

old lotus
#

ping me if you need further explanation/assistance

cold jungle
#

mkay great

#

@old lotus worked great

#

thank you man