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

Making a gui appear when player touches part?

I'm making a gui that when you touch a part it appears. I'm also trying to make it so it add one to the value so it changes from 0/10 to 1/10 etcetera. So far it doesn't make the gui appear/doesn't change the text. Here is the script I made:

local badges = {000,000,000}
local a = script.Parent.badgenumber.Value
local badgeservice = game:GetService("BadgeService")

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	game.Players.PlayerAdded:Connect(function(p)
		p.StarterGui.ScreenGui.b.Visible = true
			p.StarterGui.Screengui.Frame.Visible = true
		for i,Badge in pairs(badges) do
			wait(1)
			a.Value = a.Value +1
			p.StarterGui.ScreenGui.Frame.Text = ""..a.."/3"
		end
	end)

If you see anything wrong, please let me know.

2 answers

✓ Accepted answer

First of all, you forgot an extra "end)"

Second of all your not doing anything with "plr"

Third of all Values don't have values inside of them

Fourth of all your changing startergui

Fifth your concatenating empty strings

With all that in mind, here's new code

--[[
local badges = {000,000,000}
local badge = script.Parent.badgenumber.Value
local badgeservice = game:GetService("BadgeService")

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	game.Players.PlayerAdded:Connect(function(p)
		p.StarterGui.ScreenGui.b.Visible = true
		p.StarterGui.Screengui.Frame.Visible = true
		for i,Badge in pairs(badges) do
			wait(1)
			badge.Value = badge.Value +1
			p.StarterGui.ScreenGui.Frame.Text = ""..a.."/3"
		end
	end)
end)
--]]

--[[ Define Services ]]--
local BS = game:GetService("BadgeService") -- No pun intended
local Players = game:GetService("Players") -- Player service

--[[ Define Instances ]]--
local Part = script.Parent

--[[ Define Variables ]]--
local Badges = {[0] = true, [1] = true} -- Replace these with your badges ids, always set to true
local BadgesOwned = {}
local AlreadyCounting = false
local Success, HasBadge, Player, BadgeInfo, AwardedBadge

--[[ Define Functions ]]--
local HasBadgeFunction = coroutine.wrap(function(Player, BadgeID)
	return pcall(function(Player, BadgeID) 
		return BS:UserHasBadgeAsync(Player.UserId, BadgeID) -- Detect if user has badge...
	end, Player, BadgeID) -- ... Safely
end)

local BadgeInfoFunction = coroutine.wrap(function(Player, BadgeID)
	return pcall(function(Player, BadgeID)
		return BS:GetBadgeInfoAsyncPlayer(Player.UserId, BadgeID) -- Detect if badge is collectable...
	end, Player, BadgeID) -- ... Safely
end)

local AwardBadgeFunction = coroutine.wrap(function(Player, BadgeID)
	return pcall(function(Player, BadgeID)
		return BS:AwardBadge(Player.UserId, BadgeID) -- Award badge...
	end, Player, BadgeID) -- ... Safely
end)

local function CountBadges(Player)
	if not AlreadyCounting then 
		AlreadyCounting = true
		if #BadgesOwned[Player] >= 1 then -- If player has 1 or more badges then
			Player.PlayerGui.ScreenGui.b.Visible = true -- Enable screens
	        Player.PlayerGui.ScreenGui.Frame.Visible = true
		end
		Player.PlayerGui.ScreenGui.Frame.TextLabel.Text = string.format("%i/%i", #BadgesOwned[Player], #Badges) -- Count badges and badges owner by player and set text to them
		AlreadyCounting = false	
	end	
end

local function InitializeBadgeCount(Player) -- Start badge counting
	coroutine.wrap(function(Player) -- Make it run alongside other functions
		BadgesOwned[Player] = {}
		for BadgeID, PartOfGame in pairs(Badges) do -- Loop through badges
			Success, HasBadge = HasBadgeFunction(Player, BadgeID) -- Detect if player has badge
			for Count = 1, 5 do -- Count to 5
				if Success then -- If it's successful then...
					break -- ... stop counting
				end
				wait(4) -- Wait 4 seconds then...
				Success, HasBadge = HasBadgeFunction(Player, BadgeID) -- ... try again
			end
			if Success and HasBadge and not BadgesOwned[Player][BadgeID] then -- If it's successful, game hasn't detected a badge, and the player owns a badge, save it into memory
				BadgesOwned[Player][BadgeID] = true -- Make the game record the badges
				CountBadges(Player) -- Count the badges
			end
		end
	end)(Player) -- Initialize
end

local function GiveBadgesOnTouched(OtherPart) 
	if Player:FindFirstChild(OtherPart.Parent.Name) then -- If player is detected
		coroutine.wrap(function(Player) -- Run alongside other functions
			for BadgeID, PartOfGame in pairs(Badges) do -- Get each badge
				Success, BadgeInfo = BadgeInfoFunction(Player, BadgeID) -- Get badge information
				for Count = 1, 5 do -- You know what this does
					if Success then
						break
					end
					wait(4)
					Success, BadgeInfo = BadgeInfoFunction(Player, BadgeID)
				end
				if Success and BadgeInfo.IsEnabled and not BadgesOwned[Player][BadgeID] then -- If badge is enabled and player doesn't own the badge
					Success, AwardedBadge = AwardBadgeFunction(Player, BadgeID) -- Award badge
					for Count = 1, 5 do -- Again, you know what this does
						if Success then
							break
						end
						wait(4)
						Success, AwardedBadge = AwardBadgeFunction(Player, BadgeID)
					end
					if Success and AwardedBadge then -- If badge was awarded
						BadgesOwned[Player][BadgeID] = true -- Record badge
						CountBadges(Player) -- Count the badges
					end
				end
			end
		end)(Player) -- Initialize 
	end
end

--[[ Connect functions ]]--
Players.PlayerAdded:Connect(InitializeBadgeCount) -- Pretty obvious
Part.Touched:Connect(GiveBadgesOnTouched)

Try this:

local badges = {000,001,002}
local a = script.Parent.badgenumber.Value
local badgeService = game:GetService("BadgeService")

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		player.PlayerGui.ScreenGui.b.Visible = true
		player.PlayerGui.ScreenGui.Frame.Visible = true
		for i, badge in pairs(badges) do
			wait(1)
			a.Value = a.Value + 1
			player.PlayerGui.ScreenGui.Frame.TextLabel.Text = (a).."/3"
		end
	end
end)

1 comment

0

Thanks for trying to help, sadly it doesn't change the text on the gui.

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