Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get the player from the character that used the tool?

workspace.ClassicSword.Activated:Connect(function(player)
	local thePlayer = game.Players:GetPlayerFromCharacter(player)
	thePlayer.PlayerGui.ScreenGui.Frame.Visible = true
end)

ServerScriptService.Script:3: attempt to index nil with 'PlayerGui'

Basically when player uses the sword, a GUI should pop up, but it's not working

1 answer

✓ 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:

-- Services
local Players = game:GetService("Players")
local StarterPack = game:GetService("StarterPack")
-- Tools
local classicSword = StarterPack:FindFirstChild("ClassicSword")
-- Functions
local function EnableGUIToPlayer(whichChar)
	local player = Players:GetPlayerFromCharacter(whichChar)
	-- Show the gui to the player
	player.PlayerGui.ScreenGui.Frame.Visible = true
end

local function DisableGuiToPlayer(whichChar)
	local player = Players:GetPlayerFromCharacter(whichChar)
	-- Hide the gui to the player
	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
		-- Getting the player from the tool that was added in the character and enable his GUI
		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")
		-- Getting the player from the tool that was added in the character and enable his GUI
		local char = whichItem:FindFirstAncestorWhichIsA("Model")
		DisableGuiToPlayer(char)
	end
end
-- Conections
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.DescendantAdded:Connect(CharHasWornTool)
		char.DescendantRemoving:Connect(CharHasRemovedTool)
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.