I’m trying to make an upgrading system where the player can see their tool in an image label.
I want this image label to show the current equipped tool’s TextureId(Tool.TextureId) of the player.
SCRIPT
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
--Player variables--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
--
local AnvilGui = script.AnvilGui
local Frame = AnvilGui.Frame.Frame
local InputSlot = Frame.InputSlot
local OutputSlot = Frame.OutputSlot
local ExitButton = AnvilGui.ExitButton
--
local ScreenSize = workspace.CurrentCamera.ViewportSize
local UpgradeLabel = script.UpgradeLabel
--Connections--
local ToolEquipped
--Variables--
local CurrentButton
local function FindToolInCharacter()
for i,instance in Character:GetChildren() do
if instance:IsA("Tool") then
print(instance.TextureId,instance.Name)
return instance.TextureId
end
end
end
local Anvil = {}
ExitButton.MouseButton1Up:Connect(function()
AnvilGui.Enabled = false
Anvil:End()
end)
local function Drag(button)
return function()
local mL = UserInputService:GetMouseLocation()
local scaleX = mL.X
local scaleY = mL.Y
button.Position = UDim2.fromOffset(scaleX,scaleY)
end
end
local function ButtonHold(button) -- Fix size of button holding
return function(actionName,inputState)
if inputState == Enum.UserInputState.Begin then
RunService:BindToRenderStep("Drag",Enum.RenderPriority.Camera.Value - 1,Drag(button))
button.Parent = AnvilGui
elseif inputState == Enum.UserInputState.End then
RunService:UnbindFromRenderStep("Drag")
Anvil:CheckPosition(button)
end
end
end
function Anvil:CheckPosition(button)
local distance = (OutputSlot.AbsolutePosition - button.AbsolutePosition).Magnitude
if distance < 100 then
button.Parent = OutputSlot
elseif distance > 100 then
button.Parent = InputSlot
end
button.Position = UDim2.fromScale(0.5,0.5)
end
function Anvil:Init()
AnvilGui.Enabled = true
self:CreateButton()
self:UpdateTexture(FindToolInCharacter())
self:Connections()
end
function Anvil:Connections()
ToolEquipped = Character.ChildAdded:Connect(function(instance)
if instance:IsA("Tool") then
self:UpdateTexture(instance.TextureId)
end
end)
end
function Anvil:UpdateTexture(id)
UpgradeLabel.Image = id
end
function Anvil:CreateButton()
local new = UpgradeLabel:Clone()
new.Parent = InputSlot
ContextActionService:BindAction("Mouse",ButtonHold(new),false,Enum.UserInputType.MouseButton1)
CurrentButton = new
end
function Anvil:End()
ContextActionService:UnbindAction("Mouse")
ToolEquipped:Disconnect()
ToolEquipped = nil
CurrentButton:Destroy()
CurrentButton = nil
end
return Anvil
I’m using when the player has opened the anvil to check their inventory and when a child was added into their inventory.
I don’t understand this issue because the id in the UpdateTexture function is nil but where it came from is completely visible and real in the output.
I’m also getting the error: Unable to assign property Image. Content expected, got nil