Bug in Code. Please Help

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m currently experiencing a bug in my drag and drop inventory system, which I have no idea why it is happening. What happens is when the mouse interacts with the item, it seems to go back to its place it was before when the player dropped it into the inventory
    Attached video below for better demonstration.

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    The code used to determine where the item should go is attached below.

task.wait()
local UIS = game:GetService("UserInputService")

local item = script.Parent
 -----------
local Dragtoggle = nil
local dragSpeed = 0
local dragstart = nil
local startPOS = nil

local originalpos = nil
local startedat = nil
local endedat = nil

local tagNum = 1 -- CHANGE based on item

local function updateInput(input)
	
	local delta = input.Position - dragstart
	local pos = UDim2.new(startPOS.X.Scale, startPOS.X.Offset + delta.X,
		startPOS.Y.Scale, startPOS.Y.Offset + delta.Y)
	game:GetService("TweenService"):Create(item, TweenInfo.new(dragSpeed), {Position = pos}):Play()
end

------------------
local SlotFolder = game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("InventoryGUI").inv.Folder:GetChildren() -- getting all slots and reutrns a table

local function calculateDistance(key) -- distance calculator
	return (item.AbsolutePosition - key.AbsolutePosition).Magnitude
end
------
local function originatedats() -- determines which slot the item is closest to (used for help in determining which slot was closest when input began and ended)
	local _min = {math.huge, nil}
	for _,v in ipairs(SlotFolder) do
		local dist = calculateDistance(v)
		if dist < _min[1] then _min = {dist, v} end
	end
	startedat = _min[2]
end

local function startedats() -- determines which slot's value must be freed based on where it orginated at
	for _,v in ipairs(SlotFolder) do
		if v == startedat then
			v.Value.Value = 0
			return -- ends the function and stops it from checking the other slots
		end
	end
end
------
local function concludedats() -- determines which slot the item is closest to (used for help in determining which slot was closest when input began and ended)
	local _min = {math.huge, nil}
	for _,v in ipairs(SlotFolder) do
		local dist = calculateDistance(v)
		if dist < _min[1] then _min = {dist, v} end
	end
	endedat = _min[2]
end

local function endedats() -- determines whether or not item can be moved
	for _,v in ipairs(SlotFolder) do	
		if v == endedat then
			if v.Value.Value == 0 then
				item.Position = v.Position
			end	
			return -- ends the function and stops it from checking the other slots
		end
	end
end
-----------------

item.InputBegan:Connect(function(input)
	
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
		originalpos = item.Position
		Dragtoggle = true
		dragstart = input.Position
		item.ZIndex = 100
		
		originatedats()
		

	end
	
end)

item.InputEnded:Connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
	startPOS = item.Position
	 item.ZIndex = 1

	Dragtoggle = false
		
		concludedats()
		startedats()
		endedats()
		end
end)


UIS.InputChanged:Connect(function(input)
	
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		
		if Dragtoggle then
			updateInput(input)
			
			
		end
		
	end
	
end)

Turns out I needed to add a slight delay to the code which fixed the problem.

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