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

How do I manipulate a player's GUI from a server script?

Hello! So I have been having some problems with me and my friend's new survival game, and there's a problem where I need to access the player's GUI from my script (Non-local), but I need the PlayerGui folder so I have to use a local script. However, this is a full script with ALL of the important code in it.

-- Sorry if this is kinda a dumb error ¯\_( :) )_/¯

local SS = game:GetService("ServerStorage")
> local Player = game:GetService("Players").LocalPlayer

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "LogBreakable" then
		print("Breaking...")
		script.Parent["Wood Hit"]:Play()

>		for i = 0,100,1 do
> 			wait(0.04)
> 			Player.PlayerGui.Breaking.side.Visible = true
> 			Player.PlayerGui.Breaking.side.main.Size = i
> 		end

		print("Broke!")
		script.Parent["Falling Tree Sound Effect"]:Play()
		wait(1)
		local logC = SS.Resources.Log:Clone()
		logC.Parent = workspace
		
		if hit.Parent ~= nil then
			local pos = hit.Position
			hit.Parent:Destroy()
			logC.Position = pos
		end
	end
end)

Thanks in advance.

2 answers

✓ Accepted answer

You can get the player by using the touched event to get the players character which you can then trace back to the player itself. You can do that like this:

local SS = game:GetService("ServerStorage")
local plrs = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
     if plrs:GetPlayerFromCharacter(hit.Parent) then
          local Player = plrs:GetPlayerFromCharacter(hit.Parent)
          if hit.Name == "LogBreakable" then
             print("Breaking...")
             script.Parent["Wood Hit"]:Play()

            for i = 0,100,1 do
                wait(0.04)
                Player.PlayerGui.Breaking.side.Visible = true
                Player.PlayerGui.Breaking.side.main.Size = Udim2.new(i/100,0,1,0)
            end

             print("Broke!")
             script.Parent["Falling Tree Sound Effect"]:Play()
             wait(1)
             local logC = SS.Resources.Log:Clone()
             logC.Parent = workspace
 
             if hit.Parent ~= nil then
                 local pos = hit.Position
                 hit.Parent:Destroy()
                 logC.Position = pos
             end
         end
     end
end)

2 comments

0

I'm trying to break a tree with the axe, And its not really making the tree break. And I think that is because its trying to get the player and the log at the same time.

0

Nevermind I fixed it.

Firstly, I have to say to you, you can't access PlayerGui from Server Script (with Filtering Enabled). Secondly, you're using game.Players.LocalPlayer on a Server Script, which is very wrong. If you want to change PlayerGui, use a script firing a remote and receive on local script.

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