MouseButton1Click not working with my script?

I’m trying to set a image and label to the decal and text when clicked on a button of an item, but it is not working.



local Starterpack = game:GetService("StarterPack")
local startergui = game:GetService("StarterGui")
local serverstorage = game:GetService("ServerStorage")
local goldenknife = serverstorage:WaitForChild("GoldenKnife")
local inventoryframe = startergui.InventoryGui.InventoryFrame

local weaponimage = inventoryframe.ItemsFrame.WeaponFrame:WaitForChild("WeaponImage")
local weaponname = inventoryframe.ItemsFrame.WeaponFrame:WaitForChild("WeaponName")

local goldenname = "Golden Knife"
local goldendecal = "rbxassetid://16584185412"

local equippedframe = inventoryframe.KnifeFrame
local equippedimage = equippedframe.WeaponImage
local equippedlabel = equippedframe.WeaponLabel


if goldenknife then
	weaponname.Text = goldenname
	weaponimage.Image = goldendecal
end

equippedlabel.Text = "???"


---here is my main issue
weaponname.MouseButton1Click:Connect(function(click)
	equippedimage = goldendecal
	equippedlabel = goldenname
end)

Hello,

Can you please post any errors that may be appearing inside the Output
View > Output

Also, is this being handled inside a LocalScript? User input must be handled via a LocalScript, LocalScripts are for client only, the server doesn’t know what the user is clicking on.

You will then have to port the code to work with the local player.

Example:

This >

local inventoryframe = startergui.InventoryGui.InventoryFrame

Would turn into this >

local players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local inventoryframe = localPlayer.PlayerGui.InventoryGui.InventoryFrame

The StarterGui service is, to put it simply, a storage vessel for all the UI that needs to be replicated to each individual client when they load in. When the UI inside of StarterGui is replicated it is placed into the local player’s PlayerGui.

In order to make any changes to the client’s UI you need to access it via PlayerGui otherwise you just wouldn’t be making any changes!

Hope this helps!

References:

LocalScript | Documentation - Roblox Creator Hub
StarterGui | Documentation - Roblox Creator Hub
PlayerGui | Documentation - Roblox Creator Hub
GuiButton | Documentation - Roblox Creator Hub
Mouse and Keyboard Input | Documentation - Roblox Creator Hub

1 Like

You need to change the actual image property and text property of equippedimage and equipped label.
Currently equippedimage and equippedlabel are variables that contain instances.
Change

weaponname.MouseButton1Click:Connect(function(click)
	equippedimage = goldendecal
	equippedlabel = goldenname
end)

INTOOOO

weaponname.MouseButton1Click:Connect(function()
	equippedimage.image = goldendecal
	equippedlabel.text = goldenname
end)

(I don’t think MouseButton1Click returns anything)

@randomperson12331
That didn’t work, but thank you for trying to help.

@tannnxr
This is my only error, but it doesn’t make sense. I’ve changed it to a LocalScript and here’s my updated code.
–error
value of type string cannot be converted to a number
-code



local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer

local Starterpack = game:GetService("StarterPack")
local playergui = localPlayer.PlayerGui
local serverstorage = game:GetService("ServerStorage")
local goldenknife = serverstorage:WaitForChild("GoldenKnife")

local inventoryframe = localPlayer.PlayerGui.InventoryGui.InventoryFrame

local weaponimage = inventoryframe.ItemsFrame.WeaponFrame:WaitForChild("WeaponImage")
local weaponname = inventoryframe.ItemsFrame.WeaponFrame:WaitForChild("WeaponName")

local goldenname = "Golden Knife"
local goldendecal = "rbxassetid://16584185412"

local equippedframe = inventoryframe.KnifeFrame
local equippedimage = equippedframe.WeaponImage
local equippedlabel = equippedframe.WeaponLabel


if goldenknife then
	weaponname.Text = goldenname
	weaponimage.Image = goldendecal
end

equippedlabel.Text = "???"



weaponname.MouseButton1Click:Connect(function()
	equippedimage.Image = goldendecal
	equippedlabel.Text = goldenname
end)

I found this boilerplate from a user called, Agent_Invalid, see if it works.

local Tool: Tool = script.Parent
local Player = Players.LocalPlayer

-- Variable
local LMB_Connection: RBXScriptConnection = nil
local RMB_Connection: RBXScriptConnection = nil

-- Events
Tool.Equipped:Connect(function(mouse: Mouse)-- Connect events after equip
	LMB_Connection = mouse.Button1Down:Connect(function()
		local Origin: CFrame = mouse.Origin
		
		mouse.Button1Up:Wait()
		
	end)
	RMB_Connection = mouse.Button2Down:Connect(function()
		
		mouse.Button2Up:Wait()
		
	end)
	Tool.Unequipped:Wait() -- Disconnect events after unequip
	LMB_Connection:Disconnect()
	RMB_Connection:Disconnect()
end)

I think that in that case the script would be in the tool, but mine is a LocalScript inside the “weaponname” button I have

Is this the entire error? I can’t see why it would be having issues.

Are you sure the error is even for the local script?

Yeah no, i found out it wasnt for the localscript, something else.