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

Trying to make gui progress bar but I dont know how to get the player?

I am trying to make a backpack gui so you know how much you hold. I have a Script inside of the Bar frame.

To get the player you can't do game.Players.LocalPlayer because it's a script and not a local script. How do I get the player?

Script:

game.Players.LocalPlayer.Values.PlayerItemHolds.Changed:connect(function()
	local prog = player.Values.PlayerItemHolds
	prog.Value = prog.Value + 1
	script.Parent:TweenSize(UDim2.new(prog.Value/100, 0, 1, 0), "In", "Bounce", .1)
	if prog == 10 then
		prog = 0
	end
end)

1 answer

✓ Accepted answer

There are a few other ways of getting a player in a global script. You can use this loop to get all players:

--Do not forget to make this loop refresh every time a player leaves or joins

Global Script:

function LoopCheck()
	for i,v in pairs(game:GetService("Players"):GetPlayers()) do
		v.Values.PlayerItemHolds.Changed:connect(function()
	    		local prog = player.Values.PlayerItemHolds
	    		prog.Value = prog.Value + 1
	    		script.Parent:TweenSize(UDim2.new(prog.Value/100, 0, 1, 0), "In", "Bounce", .1)
	    		if prog == 10 then
	        		prog = 0
	    		end
		end)
	end
end

Or if you don't want to check every player one-by-one, you can use a remote event. What you will do is simple, you will fire the server from the client. You will need two scripts this time, and a remote event.

Global Script:

local Event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") --You have to create a remote event manually in order for this to work

Event.OnServerEvent:Connect(function(player)
    local prog = player.Values.PlayerItemHolds
    prog.Value = prog.Value + 1
    script.Parent:TweenSize(UDim2.new(prog.Value/100, 0, 1, 0), "In", "Bounce", .1)
    if prog == 10 then
        prog = 0
    end
end)

Local Script:

local Event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") --You have to create a remote event manually in order for this to work

game.Players.LocalPlayer.Values.PlayerItemHolds.Changed:connect(function()
	Event:FireSrver()
end)

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