How to reference PlayerGUI in a normal script inside of a tool

I’ve got this melee weapon. I want it to reduce the stamina bar inside of the Player’s PlayerGui, but I’m not sure how to reference the player and their PlayerGui inside of a normal script.

2 Likes

Get the player that’s using the Tool, then you can do something like Player.PlayerGui

1 Like

You can use Remote events to achieve this.

2 Likes

I got this, but how would I use Character to find the Player?

local Tool = script.Parent

local Character = Tool.Parent
1 Like

I believe it is,

local Character = tool.Parent
local plr = game.Players:GetPlayerFromCharacter(Character)

Never worked with tools, Could be wrong.

1 Like

You can use the Character’s name and then do something like game.Players[Character.Name] too

1 Like

It’s something like this:

local tool = script.Parent
local character = tool.Parent

local player = game:GetService("Players"):GetPlayerFromCharacter(character)
1 Like

error
what does this error mean?

1 Like

You cannot access PlayerGui in a server script, only way around this is using Remote Events or do this in a local script.

1 Like

How would I use remote events to do this? I have no idea how to use them

1 Like

Use this Remote Functions and Events

1 Like

so, I should use server to client for this instance?

1 Like
local players = game:GetService"Players"
local tool = script.Parent

tool.Equipped:Connect(function()
	local character = tool.Parent
	local player = players:GetPlayerFromCharacter(character)
	if player then
		local playerGui = player:FindFirstChildOfClass"PlayerGui"
		if playerGui then
			--Do code.
		end
	end
end)

tool.Unequipped:Connect(function()
	local backpack = tool.Parent
	local player = backpack.Parent
	local playerGui = player:FindFirstChildOfClass"PlayerGui"
	if playerGui then
		--Do code.
	end
end)

Server script inside the tool.

2 Likes