Item system not working right

Hi. Im new to using OOP in my projects, and im having an issue with an item system im working on. Theres supposed to be a proximity prompt put into a model when i call the Init() function, but it doesnt seem to work. No prompt is added.

game.ServerStorage.Modules.Item:

local Item = {}
Item.__index = Item

Item.new = function(Model)
	local Default = {}
	Default.Model = Model
	Default.Prompt = nil
	Default.Holder = nil
	Default.Tags = {}
	Default.Heavy = false
	
	Default.HolderChanged = Instance.new("BindableEvent")
	Default.Held = Instance.new("BindableEvent")
	Default.Dropped = Instance.new("BindableEvent")
	
	Default.Activated = Instance.new("BindableEvent")
	Default.Deactivated = Instance.new("BindableEvent")
	
	return setmetatable(Default, Item)
end

Item.Init = function(self)
	self.Prompt = Instance.new("ProximityPrompt", self.Model)
	self.Prompt.ActionText = ""
	
	self.Prompt.Triggered:Connect(function(Player)
		local Char = Player
		local ItemHolder = Char:FindFirstChild("ItemHolder")
		if ItemHolder then
			ItemHolder = require(ItemHolder)
		end
		
		self:SetHolder(ItemHolder)
	end)
end

Item.SetHolder = function(self, Holder)
	local OldHolder = self.Holder
	
	self.Holder = Holder
	
	if OldHolder ~= Holder then
		self.HolderChanged:Fire()
	end
	
	if OldHolder ~= nil then
		if OldHolder.HeldItem == self then OldHolder:SetItem() end
	end
	
	if Holder ~= nil then
		if Holder.HeldItem ~= self then Holder:SetItem(self) end
	end
	
	if OldHolder == nil and Holder ~= nil then
		self.Held:Fire()
	elseif OldHolder ~= nil and Holder == nil then
		self.Dropped:Fire()
	end
	
	if Holder ~= nil then
		self.Prompt.Enabled = not Holder.HidePrompts
	end
end

Item.Activate = function(self)
	self.Activated:Fire()
end

Item.Deactivate = function(self)
	self.Deactivated:Fire()
end

return Item

ModuleScript inside of a model in the workspace:

local Item = require(game.ServerStorage.Modules.Item)
local Box = Item.new(script.Parent)

Box:Init()

return Box

Didnt include my second part of the system, “ItemHolders”, because i dont think has anything to do with this issue.

Anyways, please help me here if you know something. Im not great at this stuff lol.

also if you have an idea as to how i could make this system better, please share your idea.

anyone out there?

why do my posts always pass away

1 Like

check the model is in the Workspace. If not, move it there so the ProximityPrompt can display correctly

You have a function Item.Init() but you’re calling Item:Init()…? Something there looks odd to me else idk

its in workspace, i checked. Im thinking for some reason Init() isnt getting called?

Calling a function with the " : " instead of a " . " sets the first argument to be the table which is calling the function. So this is calling the function with the “self” variable being the Box table

1 Like

Can you add prints to verify that code is running atleast at the end so it’s not thag the code is never ran, as you don’t show the script that requires the ms unless I’m blind again

Okay well i fixed it. I think module scripts dont run lines of code unless its setting a variable or function or something, but anyways i just removed everything to do with the :Init() function and just placed that code into the .new() function

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