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

How would I make in to a GUI button?

Hey I was wondering is it possible to turn this from onTouch to onClick GUI instead? ScreenGUI>Textbutton>LocalScript

How would I go about doing this? please help!

Dynamic place creation and saving

The script I want to know how to change to onClick rather than onTouch

game:GetService('TeleportService').CustomizedTeleportUI = true
 
ds = game:GetService("DataStoreService"):GetGlobalDataStore()
 
local Trigger = script.Parent
 
-- debouncer that prevents multiple trigger events by the player who is standing nearby.
function debounce(func)
    local isRunning = false
    return function(...)
        if not isRunning then
            isRunning = true
            func(...)
            isRunning = false
        end
    end
end
 
function getPlayer(Part)
	local Humanoid = Part.Parent:FindFirstChild('Humanoid')
	if (Humanoid ~= nil) then
		local Character = Humanoid.Parent
		if (Character ~= nil) then
			return game:GetService('Players'):GetPlayerFromCharacter(Character)		
		end
	end
end
 
function _sendPlayerToBuildZone(player)
	local playerIdentity = player.Name .. '(' .. player.userId .. ')'
 
	local playerKey = 'player_' .. player.userId
	local playerData = ds:GetAsync(playerKey)
 
	if playerData then
		if playerData.personalPlaceId <= 0 then
			playerData = nil
		end	
	end
 
	if not playerData then				
		newPlaceId = game:GetService(“AssetService”):CreatePlaceAsync('Building zone for ' .. playerIdentity, 92697995)
 
		playerData = {
			personalPlaceId = newPlaceId					
		}
 
		ds:SetAsync(playerKey, playerData)	
	end			
 
	if playerData and playerData.personalPlaceId then	
		game:GetService('TeleportService'):Teleport(playerData.personalPlaceId, player)			
	end	
end
local sendPlayerToBuildZone = debounce(_sendPlayerToBuildZone)
 
-- Touch event function
function onTouch(Source)
	local player = getPlayer(Source)	
	if player then
		if player.userId > 0 then		
			sendPlayerToBuildZone(player)
		end		
	end	
end
 
Trigger.Touched:connect(onTouch)

1 answer

##Introduction

Doing this is quite simple, as you already have the _sendPlayerToBuildZone function created for you. All you have left to do is modify how it is called.

To simplify this script to better understand it how the touch works, lets hide everything that doesn't contribute to the Touched event and collapse a few functions.

function debounce(func) ... end

function getPlayer(Part) ... end

function _sendPlayerToBuildZone(player) ... end
local sendPlayerToBuildZone = debounce(_sendPlayerToBuildZone)

function onTouch(Source)
    local player = getPlayer(Source)    
    if player then
        if player.userId > 0 then       
            sendPlayerToBuildZone(player)
        end
    end
end

script.Parent.Touched:connect(onTouch)

As you can see, there is only a small bit of code we have to change in order to make this work when the player clicks a GUI rather than touching a part.

##Solution

Instead of using the Touched event of a BasePart, we will instead use the MouseButton1Click event of a GUIButton. This should be used in a LocalScript to function properly

local player = game.Players.LocalPlayer
local button = GUIBUTTON_LOCATION

function debounce(func) ... end

function _sendPlayerToBuildZone(player) ... end
local sendPlayerToBuildZone = debounce(_sendPlayerToBuildZone)

button.MouseButton1Click:connect(function()
	if player.userId > 0 then
		sendPlayerToBuildZone(player)
	end
end)

Make sure that you fill in the ... in the _sendPlayerToBuildZone and debounce functions.

3 comments

0

I tried that out filled in the rest of the functions and everything, the localscript is inside textbutton itself button = script.parent

0

This shows in server log "Datastore can't be accessed from client"

0

Oh whoops, that is because DataStores can only be accessed from the sever. You could use a RemoteEvent to invoke the serever when a player clicks.

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