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

How to make a GUI tween to a UDIM2 when a part is touched?

Asked by 3 years ago

I've tried the script below, but it hasn't worked! Is anyone able to help?

script.Parent.Touched:Connect(function()
    local object = game.StarterGui.DeathScreen.ImageLabel
        object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))
end)

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Tip: Don’t change GUI directly from StarterGui in scripts when you want to update it for the player, StarterGui is a template which is cloned to the player upon being loaded in

Since I assume this is a local script, use game.Players.LocalPlayer.PlayerGui instead of StarterGui and so on, then tween the Gui in your function.

0
So, a local script within the part? theepicboy6789 56 — 3y
0
Yes zboi082007 270 — 3y
0
So, local object = game.Players.LocalPlayer.PlayerGui.DeathScreen.ImageLabel object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0)) theepicboy6789 56 — 3y
0
Define Object first and then tween it. zboi082007 270 — 3y
0
LocalScripts don't work in the workspace -_- AnasBahauddin1978 715 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Touched events on the client is not a good thing and localscripts don't work in the workspace, inside a part etc., you can use a remote event and fire it to the client to tween it for the player who touched it, assuming you already know remote events, if you don't then here:

Remote Events and Functions

put the remote event in ReplicatedStorage, you also to use GetPlayerFromCharacter which will get the player from character, if its an npc tho, it will be nil,

GetPlayerFromCharacter

first in the script inside the part, type the touched event and then use GetPlayerFromCharacter

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- if it's a character and has a humanoid then do
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if not player then return end -- If the player doesnt exist or it's an npc then stop
        game.ReplicatedStorage.RemoteEvent:FireClient(player) -- The first argument of player is required
    end
end)

then in a localscript inside the part, you have to type the remote event OnClientEvent because you fired the client (and not the server)

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui") -- Type it exactly or it will error

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() -- We don't get the player argument
    if playerGui:FindFirstChild("DeathScreen") then
        local object = PlayerGui.DeathScreen.ImageLabel
        object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))
    end
end)

Answer this question