Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

tweening gui onTouched?

Asked by 9 years ago
function onTouched()
    game.starterGui.ScreenGui.Frame:TweenPosition(UDim2.new(0.5, -250,0, 0))
end
script.Parent.onTouched:connect()
0
Please provide explanation with your code. M39a9am3R 3210 — 9y

2 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Your problem is a common mistake that many people make assuming that StarterGui changes all Player Guis. This is however not the case, since the StarterGuis are basically copied into the PlayerGui object of the Player. You also have no way of identifying if a player is hitting the brick, thus leaving the script not knowing what to do. Your script is also trying to identify "OnTouched" as an event, which it is not a valid event.

There are a couple solutions, one involving FilteringEnabled being Disabled and FilteringEnabled being Enabled.


With FilteringEnabled being Disabled, the server has more flexibility with what it can do with the client. Meaning you're able to manipulate GUIs through the server. With the Touched event, it provides the part that hit the brick being listened to. So we can try to identify the player through that using :GetPlayerFromCharacter() from the Players Service.

function OnTouched(objectThatHit)
    if objectThatHit.Parent:FindFirstChild('Humanoid') then --If there is a humanoid in the object's parent, then we know it's probably a character model.
        local player = game:GetService('Players'):GetPlayerFromCharacter(objectThatHit.Parent) --We should have the player by now. So we'll access it's PlayerGui.
        player.PlayerGui.ScreenGui.Frame:TweenPosition(UDim2.new(.5,-250,0,0))
    end
end

script.Parent.Touched:connect(OnTouched)

This script can go in a Server-Sided Script, inside the part that would need to be touched for the Gui to pop up. With FilteringEnabled Disabled. If all three fields are not met, the script will likely throw an error.


With FilteringEnabled Enabled, the server is restricted with use of PlayerGuis of Players. Which for this case, you would need to use a LocalScript.

For this to work, you would need to name the part that needs to be hit uniquely. That way the script doesn't confuse a part with another part.

And for this script, you do not need to use the GetPlayerFromCharacter method. You can simply use the LocalPlayer value of Players.

function OnTouched()
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame:TweenPosition(UDim2.new(.5,-250,0,0))
end

game.Workspace.PartNameThatNeedsToBeHitHere.Touched:connect(OnTouched)

Kind of what Andorks was trying to do with his code. You would need to have this script in a LocalScript in the PlayerGui, Backpack, PlayerScripts, or the Player's Character. Otherwise this script will not work.


If this answer helped, leave an upvote. If this answered your question, go ahead and hit the accept answer button. If you have any questions feel free to leave a comment below!
0
Nicely Done, It also made me learn! Great job. NathanAdhitya 124 — 9y
Ad
Log in to vote
0
Answered by
Andorks 23
9 years ago

You're making a common mistake. It is seen pretty easily. As you can tell below, this is in-correct:

function onTouched()
    game.starterGui.ScreenGui.Frame:TweenPosition(UDim2.new(0.5, -250,0, 0))
end
script.Parent.onTouched:connect()

Not only are you trying to tween the Gui in StarterGui, but "script.Parent.onTouched" is not a event. You should use "script.Parent.Touched:connect(onTouched)" The code below should fix your problems:

function onTouched()
game.Players.LocalPlayer.PlayerGui.Frame:TweenPosition(UDim2.new(0.5, -250,0, 0))
end
script.Parent.Touched:connect(onTouched)

Please click accept my answer, and rate me up! ;) If it doesn't, please message me!

0
You did not explain the changes to the code. Also, your code would not work since you never connected to the function. Plus the end user would most likely assume to use a server script when you're using the LocalPlayer value of Players. M39a9am3R 3210 — 9y
0
Thanks for the help. Andorks 23 — 9y

Answer this question