Help with crosshair script

  1. What do you want to achieve?
    I’m trying to make a crosshair scale that spread when humanoid jumped and reset to 0,0,0 when land

  2. What is the issue?
    The speed is not editable

  3. What solutions have you tried so far?
    I tried using spacebar and tweenservice but it’s looks ugly it’s reset when jumping but not on landing

local main = script.Parent
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(i)
 if i.KeyCode == Enum.KeyCode.Space then
        main:TweenSize(UDim2.new(10, 10, 10, 10)
     end
end)

there is more but you know what I meant

1 Like

I suggest looking at this:

2 Likes

You need to do something like this.

local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")

humanoid.StateChanged:Connect(function(oldState, newState)
    if newState == Enum.HumanoidStateType.Jumping then
        -- do your cross hair spread code here. (Also make sure you're using tween styles, etc.)
    elseif newState == Enum.HumanoidStateType.Landed then
        -- do your cross hair reset here.
    end
end)
2 Likes

Thx both of you for fixing my script and give me articles;)

Umm so i can use this type of tween?

You can edit the speed. It is a parameter of the TweenSize function:

https://developer.roblox.com/en-us/api-reference/function/GuiObject/TweenSize

Also, you can do Humanoid.StateChanged:Connect(function(newstate)) to find if the player is jumping and falling.

1 Like

Can i use the old tween?cuz idk

1 Like

Yep that would be how you change the speed of a tween. You can also make it less than 1. So something like 0.5 seconds.

1 Like

local guiObject = script.Parent
local character = player.Character
local humanoid = character:FindFirstChild(“Humanoid”)

humanoid.statechanged:Connect(function(newstate))
if newState == Enum.HumanoidStateType.Jumping then
local willTween = guiObject:TweenSize(
UDim2.new(10, 10, 10, 10),
Enum.EasingDirection.out,
Enum.EasingStyle.Linear,
2,
false,
elseif newState == Enum.HumanoidStateType.Landed then
local willTween = guiObject:TweenSize(
UDim2.new(0, 0, 0, 0),
Enum.EasingDirection.out,
Enum.EasingStyle.Linear,
2,
false,
end
end)

this?

Yep that works. Just try organizing the indentation.

Edit: i was jut looking at the overall script “logic”

1 Like

Rlly? i can’t tell bc I’m on my phone send me a video if you want to

That’s not quite right. It’s a good start, but when you use :Connect() you don’t need a extra parenthesis “)” after the function. Also everything is case sensitive, meaning that certain functions / keywords need to be capitalized a certain way. You did this on this line of code:

Your code:

humanoid.statechanged:Connect(function(newstate))

That should turn into this:

humanoid.StateChanged:Connect(function(newState)
end

See how I don’t have a extra parenthesis, and StateChanged is capitalized a certain way.

Also you don’t need to make a willTween variable. You can just do the following:

    -- Your variable doesn't need to be called guiObject, it can be called anything you name it!
    guiObject:TweenSize(
      UDim2.new(10, 10, 10, 10),
      Enum.EasingStyle.Out,
      Enum.EasingStyle.Sine,
      2
    )

Finally all of that would turn into this:

local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")

humanoid.StateChanged:Connect(function(oldState, newState)
    if newState == Enum.HumanoidStateType.Jumping then
        main:TweenSize(
            UDim2.new(10, 10, 10, 10),
            Enum.EasingStyle.Out,
            Enum.EasingStyle.Sine,
            2
        )

    elseif newState == Enum.HumanoidStateType.Landed then
        main:TweenSize(
          UDim2.new(0, 0, 0, 0),
          Enum.EasingStyle.Out,
          Enum.EasingStyle.Sine,
          2
        )
    end
end)
1 Like

Owo that is so clean looks nice

1 Like

Hmm We don’t need Script.parent?

You need to put your variable in there. So if the scripts parent is the crosshair then put

local main = script.Parent
1 Like

local main = script.Parent
local character = player.Character
local humanoid = character:FindFirstChild(“Humanoid”)

humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
main:TweenSize(
UDim2.new(10, 10, 10, 10),
Enum.EasingStyle.Out,
Enum.EasingStyle.Linear,
2
)

elseif newState == Enum.HumanoidStateType.Landed then
    main:TweenSize(
      UDim2.new(0, 0, 0, 0),
      Enum.EasingStyle.Out,
      Enum.EasingStyle.Linear,
      2
    )
end

end)

This?

1 Like

Yes that’s how it should look, so when you post code on here you want to format it into Lua code so it’s readable.

The way you do that is by doing this:

example1

Then it will look something like this on your post:


1 Like

Ik that but the problem is I’m on mobile phone


This is the same result

1 Like

Yep same result, but if you’re using that gui object a lot then you want to make it a variable so you aren’t constantly writing script.Parent over and over.

1 Like