So, I’m trying to make a script where a GUI comes onto the screen for only the player that touches a brick in the workspace. After 2 seconds, it returns to its original position. I don’t know why; I believe I’m doing this right but it’s just not working. It’s doing it for everyone instead of just that one player. Is there something I’m doing wrong?
local ito = game:GetService("Players").LocalPlayer.PlayerGui.Menu.RightSide
game.Workspace.TPer.Touched:Connect(function()
ito:TweenPosition(UDim2.new(0.523, 0,-0.19, 0),"InOut","Quart")
wait(2)
ito:TweenPosition(UDim2.new(1.055, 0,-0.198, 0),"InOut","Quart")
end)
This is false – you are able to utilize that event from a LocalScript. This can be quickly tested by utilizing print statements to see what player touched the part:
-- LocalScript inside of StarterPlayerScripts
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local part = workspace.Part
part.Touched:Connect(function(hit)
local playerCheck = Players:GetPlayerFromCharacter(hit.Parent)
if playerCheck then
print(playerCheck.Name) -- This will print the name of whoever touched the part
end
end)
Additionally, just because there’s a Humanoid inside of the Model that touched the part doesn’t necessarily mean that it’s from a player (if OP has NPCs in their game, they could end up activating the function and it wouldn’t be able to find any PlayerGui folder).
Since you’re utilizing the :GetPlayerFromCharacter method on the following line, that could be used in the first place to determine if it’s a player’s Character that stepped on the teleporter or not:
local Players = game:GetService("Players")
local Teleporter = workspace.TPer
local debounce = false -- Debounce to ensure that the main part of the function isn't activated too rapidly
Teleporter.Touched:Connect(function(hit)
if not debounce then
debounce = true
local player = Players.LocalPlayer
local playerCheck = Players:GetPlayerFromCharacter(hit.Parent) == player
-- Checks if the Instance that touched the player belongs to a player's Character and that its the same player that has this LocalScript (to prevent everyone's GUIs from Tweening when one person steps on the Teleporter)
if playerCheck then
local PlayerGui = player:WaitForChild("PlayerGui")
local Menu = PlayerGui:FindFirstChild("Menu")
local RightSide = Menu:FindFirstChild("RightSide")
if PlayerGui and Menu and RightSide then -- If all of those Instances exist, then...
-- Continue with Tweening the GuiObject
else
print(PlayerGui, Menu, RightSide) -- This will allow you to check if any of the referenced Instances above were nil
end
end
debounce = false
end
end)
No problem! Your post still worked as a solution in terms of Tweening the GuiObject – it’s just the slight misinformation that could’ve been edited as to ensure the OP was made aware that it can be utilized from a LocalScript.