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)
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
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.