Scripting Inventory Objects

Hi, it’s Potato. Last time I tried scripting an inventory, and since having that solved I’ve run into another issue. Basically my plan is for the player to be able to collect items, just for testing reasons I’ll be using a Scoobis model. So basically I want it so that when you touch or click the model, a picture of it pops up in your inventory, showing that you’ve collected it.
This is the script I’ve tried so far:

local Scoobis = script.Parent.Parent.Workspace
local InvenFrame = script.Parent
local ScoobisImage = InvenFrame.Parent
local clickdetector = Scoobis.Parent

clickdetector.MouseButton1Click:Connect(function()
	if ScoobisImage.visble == true then
	ScoobisImage.visible = false

else
	
	ScoobisImage.visible = true
end



end

I still don’t 100 percent understand variables, so I could be doing something wrong there.
This is what’s in the model.
image
This is the gui.
image
If anyone knows what I did wrong please let me know,
Thanks!

2 Likes

ClickDetectors don’t use MouseButton1Click. They use MouseClick.

3 Likes

Ok thanks, it still doesn’t seem to work though. I’ve tried changing the script to this:

local Scoobis = script.Parent.Parent.Workspace
local InvenFrame = script.Parent
local ScoobisImage = InvenFrame.Parent
local clickDetector = Scoobis.Parent

function onMouseClick()
ScoobisImage.visible = true



end
clickDetector.MouseClick:connect(onMouseClick)

Scoobis is the workspace. ClickDetector is the game itself. You’re trying to click the game. Change your directory for your variables.

Scoobis should be
local Scoobis = script.Parent.scoobis
clickDetector should be in the scoobis part. Then, do this:
local clickDetector = Scoobis.ClickDetector

1 Like

Ok thanks, I’ll try doing that later today but right now I’m not able to work on roblox studio.

1 Like

So I changed it to what you said, but it still doesn’t seem to work. Any ideas?
This is my new script:

local Scoobis = script.Parent.scoobis
local InvenFrame = script.Parent
local ScoobisImage = InvenFrame.Parent
local clickDetector = Scoobis.ClickDetector

function onMouseClick()
ScoobisImage.visible = true



end
clickDetector.MouseClick:connect(onMouseClick)

I’ve noticed when I put it in this little code block it makes the “onMouseClick” red, does that mean there’s an error?

It doesn’t mean an error. I’m fairly certain it means it is a function. Also, could I see the entire explorer list, as well as know what script you’re using? Otherwise, I’ve no idea where everything is at.

Properties are case sensitive so change this to ScoobisImage.Visible = true

I’m using a regular script, the explorer list is kinda long though, and may provide spoilers. I’m working on organizing it into folders, but here’s just the gui and scoobis object right now.
image
image
I’m sorry if this wasn’t any help.
@RetributionVI I changed it to that. but it’s still not working. I honsetly don’t know what to do anymore.
Script:

 local Scoobis = script.Parent.scoobis
local InvenFrame = script.Parent
local ScoobisImage = InvenFrame.Parent
local clickDetector = Scoobis.ClickDetector

function onMouseClick()
ScoobisImage.Visible = true



end
clickDetector.MouseClick:connect(onMouseClick)

Oh, are you trying to change the GUI using a server script…?

I don’t know what you mean by server script, I’m just using the regular script object. Would a LocalScript be better? Also I’m not exactly trying to change the GUI, I want it so that when you open the inventory gui, if you’ve clicked the object, then a picture of the object will be visible in the inventory, showing that you have collected it.

A server script is a normal script object. Server Scripts have access to the server and changes can replicate across to clients. However, server scripts cannot access the client directly. Client scripts are the LocalScript objects, which have direct access to the client but not the server. You should be handling GUIs using LocalScripts, as GUIs are under the PlayerGui folder of the LocalPlayer.

Ok thanks, when I get a chance I’ll change it to a localscript and see if it works

They can but you shouldn’t. The server won’t receive updates that the client makes. This is why other developers hammer it in that, for example, you should never handle Guis from the server unless it’s replication of a Gui.

Sorry it took so long, I’ve been kinda busy, but I changed it to a LocalScript. It still doesn’t seem to work, and i was wondering if there might be anything in the script I would need to change because it’s a localscript?
Script:

local Scoobis = script.Parent.scoobis
local InvenFrame = script.Parent
local ScoobisImage = InvenFrame.Parent
local clickDetector = Scoobis.ClickDetector

function onMouseClick()
ScoobisImage.Visible = true



end
clickDetector.MouseClick:connect(onMouseClick)

Ok So I assume you are doing this. Say you get an item by clicking on it. You click Scoobis.

I actually use a click detector inside my code.

This script is inside the part I want to click.

local Clickable = script.Parent

local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = Clickable
ClickDetector.MaxActivationDistance = 20
 

ClickDetector.MouseHoverEnter:Connect(function()
	Clickable.BrickColor = BrickColor.new("Neon orange")
end)
 

ClickDetector.MouseHoverLeave:Connect(function()
	Clickable.BrickColor = BrickColor.new("CGA brown")
end)


ClickDetector.MouseClick:Connect(function()
	--run my code
end)


That then will do what I want.

From what I can gather, you then want it to access your Gui, and tell it to show an image in a slot.

You’re gui in game is in a player. You have to get the player name and such to get it’s address. Something like Players.Steve_Speedy.PlayerGui.GuiTest

Don’t use spaces. Capitalise every word.

Now what you can do is a use RemoteEvent:FireClient to send a message to the Client to update the Gui.

I have an intValue and use 0 as false and 1 as true. I had some trouble with Boolean but maybe that works too. Anyway the value in the gui can then be changed. You can get the player name from the click event too.

So when you click, it gives you the player name who clicked. Then you change the intValue in their gui in their player to include the image. When they open their gui in their player, it reads the intValues and sets up the Gui based on those intValues.

Point being, Your PLAYER Gui sets itself up based on a bunch of values. When you do something in the worldspace to say “Item has been acquired by player” that thing in the worldspace sends a tiny message to the Gui in the Player, saying “This item is Acquired”. Simple as that.

Your click detector is not INSIDE the scoobis PART. Your Part Script isn’t in the PART. Have them in the part that. The actual object. It’s like a school bag. You put all your stuff you need for school INSIDE the bag.

Aditionally, you should have your LocalScript for your Gui as a direct child of the Gui. So “GuiTest.GuiScript” Remember no spaces.

After that you should be pretty set. :smiley:

1 Like

Ok thanks, it’ll take me a little bit to get it set up and understand it and if I need any help I’ll let you know.

I’ve found the instructions a little confusing, but someone on Discord reached out and helped me so I’m all set. Thanks for your help though!