✓ Accepted answer
It is always better to edit things that you want to appear locally for the player from local scripts.
The better option would have been to simply make a local script and show the gui from the local script rather than doing this from the server. But since you asked, here is the solution:
If you were to keep it as a server script (to get the player that activated a specific tool from the server), you would have to add a connection for each character that is going to join the game. Next you would have to create another connection that will detect if a descendant is added to the character ( whichChar.DescendantAdded:Connect(function(descendant) ) and if it detects that the specific tool we're looking for is added then it runs the function which will show the GUI to the player that activated the tool. If in the other hand a descendant is removing then it will hide the GUI to the player that disactivated the tool.
Here's what it would look like:
local Players = game:GetService("Players")
local StarterPack = game:GetService("StarterPack")
local classicSword = StarterPack:FindFirstChild("ClassicSword")
local function EnableGUIToPlayer(whichChar)
local player = Players:GetPlayerFromCharacter(whichChar)
player.PlayerGui.ScreenGui.Frame.Visible = true
end
local function DisableGuiToPlayer(whichChar)
local player = Players:GetPlayerFromCharacter(whichChar)
player.PlayerGui.ScreenGui.Frame.Visible = false
end
local function CharHasWornTool(whichItem)
if not whichItem:IsA("Tool") then return end
if whichItem.Name == classicSword.Name then
local char = whichItem:FindFirstAncestorWhichIsA("Model")
EnableGUIToPlayer(char)
end
end
local function CharHasRemovedTool(whichItem)
if not whichItem:IsA("Tool") then return end
if whichItem.Name == classicSword.Name then
print("Char removed sword")
local char = whichItem:FindFirstAncestorWhichIsA("Model")
DisableGuiToPlayer(char)
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
char.DescendantAdded:Connect(CharHasWornTool)
char.DescendantRemoving:Connect(CharHasRemovedTool)
end)
end)