Why is it offsetting wrong on the server?

hello,

So i’m making a game where you build,
but for some reason on the client, the ghost object is offsetted perfectly but the server part it spawns is offsetted horribly.
i don’t know why.

server:

local Event = script.Parent:WaitForChild("Task")

local function Offset(Pos :  Vector3? , Surface : Enum.NormalId?,Clone : Instance?)
	if Surface == Enum.NormalId.Top then
		return Pos + Vector3.new(0,Clone:GetAttribute("YOffset"),0)
	elseif Surface == Enum.NormalId.Bottom then
		return Pos - Vector3.new(0,Clone:GetAttribute("YOffset"),0)
	elseif Surface == Enum.NormalId.Front then
		return Pos - Vector3.new(0,0,Clone:GetAttribute("ZOffset"))
	elseif Surface == Enum.NormalId.Back then
		return Pos + Vector3.new(0,0,Clone:GetAttribute("ZOffset"))
	elseif Surface == Enum.NormalId.Left then
		return Pos - Vector3.new(Clone:GetAttribute("XOffset"),0,0)
	elseif Surface == Enum.NormalId.Right then
		return Pos + Vector3.new(Clone:GetAttribute("XOffset"),0,0)
	end
end

Event.OnServerEvent:Connect(function(plr,Pos,Surface,Name)
	local Clone = game.ServerStorage.Assets:FindFirstChild(Name)
	if not Clone then return end
	local Counter = 0
	local Selected = {}
	for i,v in pairs(plr.Backpack:GetChildren()) do
		if v.Name ~= Clone:GetAttribute("Ingredient") then continue end
		if Counter >= Clone:GetAttribute("Amount") then break end
		Counter += 1
		table.insert(Selected,v)
	end
	if Counter < Clone:GetAttribute("Amount") then return end
	for i,v in pairs(Selected) do
		v:Destroy()
	end
	table.clear(Selected)
	Clone = Clone:Clone()
	Clone.Parent = workspace
	Clone.Position = Offset(Pos,Surface,Clone)
end)

client:

local Folder = script.Parent
local Event = Folder:WaitForChild("Task")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Replicatedstorage = game.ReplicatedStorage
local UI = Player.PlayerGui:WaitForChild("BuildUI")
local Tool = Folder.Parent

local Clone = nil
local Connection = nil
local KeyConnection = nil

local function Offset(Pos :  Vector3? , Surface : Enum.NormalId?)
	if Surface == Enum.NormalId.Top then
		return Pos + Vector3.new(0,Clone:GetAttribute("YOffset"),0)
	elseif Surface == Enum.NormalId.Bottom then
		return Pos - Vector3.new(0,Clone:GetAttribute("YOffset"),0)
	elseif Surface == Enum.NormalId.Front then
		return Pos - Vector3.new(0,0,Clone:GetAttribute("ZOffset"))
	elseif Surface == Enum.NormalId.Back then
		return Pos + Vector3.new(0,0,Clone:GetAttribute("ZOffset"))
	elseif Surface == Enum.NormalId.Left then
		return Pos - Vector3.new(Clone:GetAttribute("XOffset"),0,0)
	elseif Surface == Enum.NormalId.Right then
		return Pos + Vector3.new(Clone:GetAttribute("XOffset"),0,0)
	end
end

Tool.Equipped:Connect(function()
	KeyConnection = UserInputService.InputBegan:Connect(function(obj,bool)
		if bool then return end
		if obj.KeyCode == Enum.KeyCode.H then
			if UI.Enabled then
				UI.Enabled = false
			else
				UI.Enabled = true
			end
		end
	end)
	Connection = RunService.Heartbeat:Connect(function(delta)
		local Attr = Tool:GetAttribute("SelectedItem")
		if not Clone then
			Clone = Replicatedstorage.GhostAssets:FindFirstChild(Attr)
			if not Clone then return end
			Clone = Clone:Clone()
			Clone.Parent = workspace
		end
		if Clone.Name ~= Tool:GetAttribute("SelectedItem") then
			Clone:Destroy()
			Clone = Replicatedstorage.GhostAssets:FindFirstChild(Tool:GetAttribute("SelectedItem")):Clone()
			Clone.Parent = workspace
		end
		Clone.Position = Offset(Mouse.Hit.Position,Mouse.TargetSurface)
	end)
end)

Tool.Activated:Connect(function()
	Event:FireServer(Mouse.Hit.Position,Mouse.TargetSurface,Tool:GetAttribute("SelectedItem"))
end)

Tool.Unequipped:Connect(function()
	UI.Enabled = false
	Connection:Disconnect()
	KeyConnection:Disconnect()
	Tool:SetAttribute("SelectedItem","")
	if Clone then Clone:Destroy() end
	Clone = nil
end)

the offset function which is used to get offset , is copy pasted from the client to the server so it is weird that it ony works on the client (obviously copy pasted but with a bit of changes to make it compatible for the server)

some images that show the problem:


client offset

server offset

Thanks in advance!

Try sending the Clone.Position as one of the parameters in the event. And then on the server side just have the event place it there, instead of doing the math on the client and server side.

2 Likes

thank you!!!
i did not think of that.
it works now !!!

1 Like

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