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

How to Animate a Gui After Touching a Brick?

Asked by 4 years ago

I've tried the code below to make a gui, that is placed above the screen, move to the center of the screen. The visibility of the gui is checked.

local plr = game:GetService("Players").LocalPlayer
local frame = plr.PlayerGui.mainGui.shopFrame

script.Parent.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum ~= nil then
        if frame.Position == UDim2.new(0.355,0,-0.4,0) then
            frame:TweenPosition(UDim2.new(0.355,0,0.4,0),"Out","Sine",1,true)
        end
    end
end)

The code is in a normal script. (Not local or module.)

0
If it didn't work then there may be a simple spelling error(writing code on the website isn't the mouse user friendly.). Also even if CanCollide is still on it will register the Touched event. You should check the Output for errors. namespace25 594 — 4y
0
The only error that is displayed in the output is "mainGui is not a valid member of PlayerGui" I was able to eliminate the error by replacing the code in line 6 with "local shop = playerGui:WaitForChild("mainGui")" Although, even with no error in the output, nothing seems to happen upon touching the brick. I've checked over the scripts for spelling errors, but none were found. awesomesauce905 4 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

So for this, you will need to work with RemoteEvents. The remote is event is needed because you cannot get the LocalPlayer from a server script. Only a client script(local script)

OK so first create a new RemoteEvent this can be done by hovering your mouse over ReplicatedStorage and clicking the little plus button that shows up. Once you click it type RemoteEvent then click the result that comes up to add it. Once that is done you can name it whatever but I will keep it at remote event for this tutorial.

So, once you have your event you're going to need a LocalScript to detect when the server tells the client to do something from that remote event.

So put a new Local Script into StarterPlayer >> StarterPlayerScripts and we're going to write this code in it:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui or player:WaitForChild("PlayerGui")
local shop = playerGui.mainGui
local frame = shop:WaitForChild("shopFrame")

remoteEvent.OnClientEvent:Connect(function()
    if frame.Position == UDim2.new(0.355,0,-0.4,0) then
            frame:TweenPosition(UDim2.new(0.355,0,0.4,0),"Out","Sine",1,true)
        end
end)

OK so basically what that does is listens for the remote event that we put into ReplicatedStorage be called upon by the server to the client(this is a great way because it cannot be exploited!)

So for our server script go ahead and use the script you had in your part and write this new code in it:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player then
        remoteEvent:FireClient(player)
    end
end)

And in that script, we just check to see if a real player is touching the part and if they are then we use the FireClient to activate the remote event. We use GetPlayerFromCharacter on hit.Parent to find the character model(not the real player) and that function of Players returns the player if they exist.

Learn More

0
Unfortunately, this script didn't succeed in animating the gui. I tried it once, and after it didn't work, I copied and pasted the code that you provided into the corresponding scripts. I'm not sure what the problem is. Maybe it is because CanCollide is on. awesomesauce905 4 — 4y
Ad

Answer this question