Userinputservice script

  1. What do you want to achieve? am trying to make a userinput service that makes a part with surface gui that you can interact with appear using tweenservice and disappear if i click the same key again but not like double click and i want to use a key code such as E not a mouse click but i dont know where to even begin

anything would be helpful thank you in advance <3

You don’t know where to begin?
What about the documentation?? That should be your first thought when you don’t know something about any api, Roblox’ or not :astonished:.

Anyway, here’s my script:

local tweenservice = game:GetService "TweenService"
local userinputservice = game:GetService "UserInputService"

local part = --your part here

userinputservice.InputBegan:Connect(function(already_processed: boolean, input: InputObject)
     if already_processed then return end
     if input.KeyCode ~= Enum.KeyCode.E or input.UserInputState ~= Enum.UserInputState.Begin then return end

     tweenservice:Create(part, TweenInfo.new(1),--add your tween parameters here
          {["Transparency"] = part.Transparency == 1 and 0 or 1})--allows for toggling visibility
     :Play() --play the result
end)

I’m assuming you mean I don’t have to instantiate a surface Gui, it is already in the part, and with appear you mean transparency? Because there are a lot of ways to make parts appear, by moving them in the player’s vision, by lowering the transparency, by just instantiating them in front of the player, etc…

Here’s an implementation with the other input utility service called: ContextActionService

local tweenservice = game:GetService "TweenService"
local contextactionservice = game:GetService "ContextActionService"

local part = --your part
contextactionservice:BindAction("show_part", function(_, state, obj) 
    if state ~= Enum.UserInputState.Begin then return end
    tweenservice
       :Create(part, TweenInfo.new(1), {["Transparency"] = part.Transparency == 1 and 0 or 1})
    :Play()
end, false, Enum.KeyCode.E)

also you might want to change properties on the server instead, idk if you want that, maybe you only want one player to see the part idk. But if you want all players to see the part you need to send a remote event call and perform the actions on the server:

--localscript
local remote = --your remote
local userinputservice = game:GetService "UserInputService"

userinputservice.InputBegan:Connect(function(already_processed: boolean, obj: InputObject)
      if already_processed or obj.KeyCode ~= Enum.KeyCode.E then return end
      remote:FireServer()
end)

--serverscript
local tweenservice = game:GetService "TweenService"

local remote = --your remote
local part = --your part

remote.OnServerEvent:Connect(function(plr: Player)
    tweenservice
       :Create(part, TweenInfo.new(1), {["Transparency"] = part.Transparency == 1 and 0 or 1})
    :Play()
end)

Learn more:

(tell me if I misunderstood something, idk)
Hope this helps!

1 Like

yes!, thank you so much the second script you’ve made was the exact outcome i wanted i appreciate the time and the help dude hope you have a good rest of your day

1 Like

but could you help me with 1 more thing instead of the transparency tween i want it to be the size when it appears the size is gonna be like vector3.new(3,3,3)
and when it disappears it’s gonna be like vector3.new(0,0,0)
the part is actually a clone from replicated storage i just need help with the size thanks again!

1 Like

oh you can just change the parameters in the tween:

--also added back easing style for more cartoon feel, remove if you want
tweenservice
   :Create(part, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.InOut), {["Size"] = part.Size.X == 3 and Vector3.new() or Vector3.new(3, 3, 3)})--only comparing x because we can assume the rest is 3 aswell.
:Play()

tweenservice documentation

1 Like