Property not working?

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 making a fishing game, and am currently at the casting stage where I tween the size of a progress bar up based on how long the player holds MouseButton1, and I want to make the bar reset its size when I stop holding it down.

  2. What is the issue? Include screenshots / videos if possible!
    For some reason this error is triggered when I start holding down MouseButton1:
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried rewriting the code.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

My Script: (LocalScript)

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local player = Players.LocalPlayer
local activeTween

local function createTween(object, tweenInfo, properties)
	local tween = TweenService:Create(object, tweenInfo, properties)
	local activeTween = tween
	tween:Play()
	return tween
end

local function UpdateBarColor(bar)
	local size = bar.Size.Y.Scale
	local red = math.clamp(0 - size, 0, 0)
	local green = math.clamp(size, 0, 1)
	bar.BackgroundColor3 = Color3.new(red, green, 0)
end

local function animateBar(bar)
	bar.Size = UDim2.new(Bar.Size.X.Scale, 0, 0, 0)
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true)
	
	local tween = createTween(bar, tweenInfo, {
		Size = UDim2.new(bar.Size.X.Scale, 0,1,0),
		Position = UDim2.new(bar.Position.X.Scale)
	})
	
	return tween
end

UserInputService.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print("Began")
			
			local UI = player.PlayerGui.FishingUi
			local Main = UI.Main
			local Casting = Main.Casting
			local Bar = Casting.Bar
			
			Casting.Visible = true
			
			animateBar(Bar)
			
			task.spawn(function()
				while Casting.Visible do
					UpdateBarColor(Bar)
					task.wait(0.01)
				end
			end)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gpe)
	if not gpe then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print("Ended")
			
			local UI = player.PlayerGui.FishingUi
			local Main = UI.Main
			local Casting = Main.Casting
			local Bar = Casting.Bar
			
			Casting.Visible = false
			
			if activeTween then
				activeTween:Cancel()
			end
			
			Bar.Size = UDim2.new(Bar.Size.X.Scale, 0, 0, 0)
			Bar.Position = UDim2.new(Bar.Position.X.Scale, 0, 0, 0)
		end
	end
end)
2 Likes

typo maybe
try replacing Bar with bar
UDim2.new(bar.Size.X.Scale, 0, 0, 0)

1 Like

Attempt to index nil with size means that you are trying to get the size of something that doesn’t exist.

Make sure the object you are trying to get the size of actually exists, and is correctly stored in the variable, before running any code associated with it.

2 Likes

as someone said above there was probably a little capitalisation error replace line 23 with bar.Size = UDim2.new(bar.Size.X.Scale, 0, 0, 0)

2 Likes

This worked! Typos can be so annoying with code because it’s so hard to find them! Thank you so much for the help :slight_smile:

2 Likes

no problem, @V0RTEXwhoEditz

char limit

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