I’m currently working on a game, where you can pick up / drop items. To pick them up, I create a weld to the player, and to drop them, I remove the weld. For some reason though, when you drop an item it gets really laggy on the client. If there’s a fix to this or possibly a better way to go about doing this, it would be appreciated :>
Server Code
--IsHolding and ItemHeld are variables stored in the player.
--IsHolding is a Boolean and ItemHeld is an ObjectValue
Remotes.SetItem.OnServerEvent:Connect(function(Player, Character, Item, IsHolding, ItemHeld, Type)
if Type == "Grab" then
IsHolding.Value = true
ItemHeld.Value = Object
local ObjectWeld = Instance.new("Weld", Object)
ObjectWeld.Name = "CharacterWeld"
ObjectWeld.Part0 = Torso
ObjectWeld.Part1 = Object.PrimaryPart
ObjectWeld.C0 = Object:GetAttribute("ObjectOffset")
elseif Type == "Drop" then
IsHolding.Value = false
ItemHeld.Value = nil
local Weld = Object:FindFirstChild("CharacterWeld")
if Weld then
Weld:Remove()
end
end
end)
Make sure the remote event isn’t being fired more than once, if you want to test if it is place a print in the remote event and then test the game.
1 Like
That definitely isn’t the issue.
Can you show me the client sided code.
Pretty much just this function
local function GrabItem(Item)
Remotes.SetItem:FireServer(Character, Item, IsHolding, ItemHeld, Type)
end
Can you show me where the function is being called
This is pretty much it
local function GrabItem(Object)
game.ReplicatedStorage.Remotes.SetItem:FireServer(Variables, Object, Torso, "Grab")
end
local function CheckClosestCounter()
local closestDistance
local closestCounter
for _, Counter in pairs(Counters:GetChildren()) do
local CounterPosition = Counter.CounterPosition
local counterDirection = (CounterPosition.Position - RootPart.Position).Unit
local playerFacingDirection = Player.Character.HumanoidRootPart.CFrame.LookVector.Unit
local dotProduct = counterDirection:Dot(playerFacingDirection)
local currentDistance = (RootPart.Position - CounterPosition.Position).Magnitude
local facingThreshold = -0.1
if Counter:GetAttribute("CounterClass") and Counter:GetAttribute("CounterClass") ~= "Corner" and currentDistance <= 6 then
if closestDistance == nil or currentDistance < closestDistance then
if dotProduct >= facingThreshold then
closestCounter = Counter
closestDistance = currentDistance
end
end
end
end
return closestCounter
end
game["Run Service"].RenderStepped:Connect(function(delta)
ClosestCounter = CheckClosestCounter()
Highlight.Parent = ClosestCounter
Variables.ClosestCounter.Value = ClosestCounter
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == InteractBind then
if ClosestCounter and not HasItem.Value then
local CounterClass = ClosestCounter:GetAttribute("CounterClass")
if CounterClass == "Crate" then
local CrateItem = ClosestCounter:GetAttribute("CrateItem")
local NewItemClass = GetItemByClass(CrateItem)
local NewItem = game.ReplicatedStorage.Remotes.CreateItem:InvokeServer(NewItemClass)
local ObjectOffset = NewItem:GetAttribute("ObjectOffset")
NewItem:PivotTo(Torso.CFrame * ObjectOffset)
GrabItem(NewItem)
end
elseif ClosestCounter and HasItem.Value then
print("Place on counter")
elseif not ClosestCounter and HasItem.Value then
game.ReplicatedStorage.Remotes.SetItem:FireServer(Variables, Variables.Item.Value, Torso, "Drop")
end
end
end)
Haven’t gotten much help recently, but I noticed that it works fine in game, just not in studio. Hopefully it will work fine like this :P