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

how would I make this script work in a global script instead of a local script?

I am trying to make this local script into a global script, whenever I try to run this script in a global script it does not work. Can someone help me?

TextBox = script.Parent

r1 = script.Parent.Parent.Room1
r1.BackgroundColor3 = BrickColor.new("Mint").Color
local room1 = game:GetService("ReplicatedStorage"):WaitForChild("Room1")

hasTouched = false

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	
	r1.MouseButton1Click:Connect(function()
		if hasTouched == false then
			hasTouched = true
			
			local text = TextBox.Text
			r1.BackgroundColor3 = BrickColor.new("Persimmon").Color
			room1:Clone().Parent = game.Players:WaitForChild(text).Backpack
			print("Gave room 1 to ",text)
		end
	end)
end)

local Players = game:GetService("Players")

game.Players.PlayerRemoving:Connect(function(player)	
	local backp = player:WaitForChild("Backpack")
	local room1 = backp:WaitForChild("Room1")

	
	if room1 then
		print("Removed room 1 from backpack")
		hasTouched = false
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)	
	local rom1 = game.Workspace:WaitForChild(player.Name)
	local romm1 = rom1:WaitForChild("Room1")
	
	if romm1 then
		print("Removed room 1 from hotbar")
		hasTouched = false
	end
end)

1 answer

✓ Accepted answer

This script will not work in one script, you need a local and a global script.

Things like TextBoxes or any GUI specific shall be in a LocalScript. (Global scripts can't access the GUIs from Players)

If you want to let Global and Local Scripts communicate, you can use RemoteEvents and BindableEvents.

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

I would recommend you reading this article.

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