Scripting for pickup showing up on GUI not working?

Introduction

I’m Ithurius, a decent programmer trying to achieve dreams.


The 3 mandatory answers of What I want to achieve, the issue, and solutions tried

I’m trying to achieve a pickup for my game that edits the GUI to show accuracy in what is had

I’ve tried different arguments from the errors the output window showed, it didn’t update the GUI.

I’ve tried making variables to create arguments for the PickupEvent to transfer work to the GUI.

I’ve tried everything logical in my scripting skill range.


The scripts I made to try to get what I wanted to achieve

ChangeAmount(The local script for the GUI):

local AMOUNT = script.Parent
local STATUS = script.Parent.Parent.Status

local function ChangeAmountGUI()
	AMOUNT.Text = "1"
	if AMOUNT.Text == "0" then
		STATUS.Text = "Empty!"
		AMOUNT.Text = "0 / 10"
	end
	if AMOUNT.Text == "9" then
		STATUS.Text = "Over half full."
		AMOUNT.Text = "9 / 10"
	end
	if AMOUNT.Text == "8" then
		STATUS.Text = "Over half full."
		AMOUNT.Text = "8 / 10"
	end
	if AMOUNT.Text == "7" then
		STATUS.Text = "Over half full."
		AMOUNT.Text = "7 / 10"
	end
	if AMOUNT.Text == "6" then
		STATUS.Text = "Over half full."
		AMOUNT.Text = "6 / 10"
	end
	if AMOUNT.Text == "5" then
		STATUS.Text = "Halfway full."
		AMOUNT.Text = "5 / 10"
	end
	if AMOUNT.Text == "4" then
		STATUS.Text = "Under halfway full."
		AMOUNT.Text = "4 / 10"
	end
	if AMOUNT.Text == "3" then
		STATUS.Text = "Under halfway full."
		AMOUNT.Text = "3 / 10"
	end
	if AMOUNT.Text == "2" then
		STATUS.Text = "Under halfway full."
		AMOUNT.Text = "2 / 10"
	end
	if AMOUNT.Text == "1" then
		STATUS.Text = "Under halfway full."
		AMOUNT.Text = "1 / 10"
	end
	if AMOUNT.Text == "10" then
		STATUS.Text = "Full!"
		AMOUNT.Text = "10 / 10"
	end
end

game.ReplicatedStorage.PickupEvent.OnClientEvent:Connect(ChangeAmountGUI)

CollectScript(normal script in workspace parented to a part with an image on it):

local PlayersService = game:GetService("Players")
local PlayerObject = PlayersService.LocalPlayer

local function onTouch(hit)
	game.ReplicatedStorage.PickupEvent:FireClient(PlayerObject)
end

script.Parent.Touched:Connect(onTouch)

That’s it, please consider nudging me in the right way so I can achieve dreams. :smiley:

A normal script in Workspace can’t access the LocalPlayer. A more correct way of handling this would be the following

local function onTouch(hit)
	
	-- First we check or the hit block is part of a parent (in this case the character that we need)
	if type(hit.Parent) == "userdata" then 
		
		-- Check or our parent has a valid Humanoid, if it does we either have a player or a NPC that has a Humanoid inside it
		if hit.Parent:findFirstChild("Humanoid") then 

			-- Let's see or we have a valid player..
			-- https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter
			local player 			=		game:GetService("Players"):GetPlayerFromCharacter(hit.Parent);
			if type(player) == "userdata" then 
				game:GetService("ReplicatedStorage").PickupEvent:FireClient(player);
			end 
		end 
	end 
end

script.Parent.Touched:Connect(onTouch)
2 Likes

Let’s also make this a bit cleaner…

local AMOUNT = script.Parent
local STATUS = script.Parent.Parent.Status
local CURRENT = 0;								-- Our current amount !

local function ChangeAmountGUI()

	-- We add +1 to our current here (the variable in the top section)
	CURRENT 						=		CURRENT + 1;

	-- We now can put this to our AMOUNT label
	AMOUNT.Text 					=		CURRENT.."/ 10";

	-- Let's now make a clever if statement for our STATUS label
	local state 					=		"Empty";

	if CURRENT > 0 and CURRENT <= 4 then 
		state 						=		"Under halfway full.";
	elseif CURRENT == 5 then 
		state 						=		"Halfwall full.";
	elseif CURRENT >= 6 and CURRENT <= 9 then 
		state 						=		"Over half full.";
	elseif CURRENT == 10 then 
		state 						=		"Full !";
	end 

	-- Now let's parse this back to our text label !
	STATUS.Text 					=		state;
end 


game:GetService("ReplicatedStorage").PickupEvent.OnClientEvent:Connect(ChangeAmountGUI)
2 Likes

Your code worked for the most part, but the number went higher than 10, which is not what I wanted. Could you send a fixed script? Sorry I just need help, I have no idea how to fix this. And I’m not the best at scripting.

Hi there,

Not to become rude here, but in the OP you clearly say that you are a decent programmer. Please look a bit more inside basic programming like if statements. I expect from you that you can solve that yourself if you state that in the OP.

You just have to add an if statements that does this…

If CURRENT <= 10 then 
-- Do our code update
end
2 Likes

I tried that just now, and the script errored and didn’t work.

Hi there,

Then please copy the error that you saw in the output and paste it here.

I configured the script you used to help me out and now it works! Thank you so much!

1 Like