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

How can I enable a script when a player touches a brick?

I'm making a horror game and I have made a script In a text label, Which I have disabled Because I don't want the scripts to instantly start when you join the game. I want it to Get enabled when a player touches a certain part. How can I do this? I have searched in google and Dev forums etc. but I didn't quite find what I needed.

Here is the script I wrote But it Still doesn't work, Is there any other way for me to type it?


local Text = game.StarterGui.ScreenGui.TextLabel
local StarterGUI = game.StarterGui 
local ScreenGUI = game.StarterGui.ScreenGui

function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if h~=nil then 
		ScreenGUI.Enabled = true
		Text.Script.Enabled = true
	end
end 
script.Parent.Touched:connect(onTouched)

1 comment

0

Coding is case sensitive, so you want to make all your words capitalized correctly.

3 answers

Do this [Make sure screengui is placed in that part. And make sure screengui is disabled and your label is visible.

function touch(hit)

	if game.Players:findFirstChild(hit.Parent.Name) ~= nil then

		player = game.Players[hit.Parent.Name]

		if player.PlayerGui:findFirstChild("here") == nil then -- change here to ur screengui name

			gui = script.Info:clone()

			gui.Parent = player.PlayerGui

			repeat

				wait() 

			until (player.Character.Torso.Position - script.Parent.Position).magnitude > 5

			gui:remove()

		end

	end	

end



script.Parent.Touched:connect(touch)

All these scripts the code in them is to connect the script to start

13 comments

0

Yes I did try that, But It still doesn't work,

0

Yes I did try that, But It still doesn't work I edited the message and put what Script I made, But It still doesn't work. I don't quiet understand that.

0

Try rearranging the code like put all the locals after my script of code, the code that I showed you is simply when a player touches part it activates the code

0

is there any errors btw

0

Here's How I typed it but It still doesn't work, Did I do something wrong?

0

No there is no error, Also Do you mean I put the locals in "--ur code's' place? Because I tried that and It still didn't work. Am I typing it wrong or something, I don't quiet understand.

0

I will try it in my studio rn

0

I updated the awnser try it

0

It says "Info is not a valid member of Script".

0

change info to the name if your screengui

0

of*

0

It works by showing the GUI But there is one problem, It does clone The ScreenGUI and even the script. But when I touch the part, The script already ended. How can I do it so like when it gets touched THEN it Starts.

0

try getting rid of the gui:remove() part and make sure there are the right amount of ends. If it doesn't work I will try tomorrow getting it to work.

You actually can't enable/disable a script once the game starts with the properties, it just determines if it will be enabled or disabled once the game does start.

Instead, I think you might want to consider just having the script you want to enable have a function that only get's called when the part get's touched? If the script had like a loop or something that you want to run after the part get's touched you could have the touched function set a bool value to true and let the loop run from there.


local enable = false --bool value for if the loop is enabled

part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h~=nil then
		enabled = true --set enabled to true, which would let a loop run
		--alternatively if the script just needed to do something once you could just call a function here 
		script()
	end
end)

while (enabled == true) do
	--whatever the loop you had in the other script did
end

function script()
	--whatever the script needs to do
end

I think this would be a much cleaner way to go about solving your problem then trying to enable/disable scripts, and I hope it helps!

You can't really run a script after enabling it. While the other 2 answers will work, here's another way. You can use the loadstring() global function. What it does is the string in the parameter of the function will be converted into a function. And if you run that function, it will actually do something. For example:

local printSomething = loadstring("print('Something')")
printSomething() -- prints "Something"

So the solution for this method is to type the quotation marks inside the parameter first, then inside the quotation marks, paste your code, and call it as a function.

local doCode = loadstring(" paste your code here ")
doCode()

But it's not recommended to enable this function because hackers can easily run a hack module in your game using that function.

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