Help a new scripter with writing a script

Hello there! I am having a bit of trouble trying to make a script for my game. I am very new to scripting and still am in the process of learning lua.

I am working on making a computer which the player can interact with. Currently, there are two screens that the computer can switch between. I am trying to find a way to make Screen 1 (Floppa) switch to Screen 2 (Bluescreen) when it is touched by the player. Here are the screens…

cf1
Screen 1 (Floppa)

cb2
Screen 2 (Bluescreen)

And here is everything you need to see in my workspace.

workspace

As you can see, I made two different parts for the screens. This is because both the Floppa and Bluescreen screens are a decal. Essentially, my hope was to keep things simple. I wanted to make two scripts where, when the first screen is touched, it turns transparent and the second screen becomes completely visible. Here is my code for Decal1 (Aka the Floppa picture):

local Screen = script.Parent.Parent -- screen in which the Floppa picture is on

local Floppa = script.Parent -- decal1 (the decal which is supposed to turn transparent on touch.)

Screen.Touched:connect(function()
	
	Floppa.Transparency = 1
	
	Screen.Transparency = 1 -- turned both the part for the screen and the decal1 transparent because i have two different parts for the screen.
	
end)

end

And for the Bluescreen, or Decal2:

local Screen = script.Parent.Parent -- the screen decal2 is on.

local BlueScreen = script.Parent -- decal2. this decal is supposed to become opaque.

Screen.Touched:connect(function()
	
	BlueScreen.Transparency = 0
	
	Screen.Transparency = 0 -- again, made them both opaque for the same reason as before, just to make things simpler
	
end)

end

I also wanted to make this change only visible to the player touching it, hence the local script instead of a regular script.

My scripts do not seem to work. There are no error messages or anything in the output feed, but nothing seems to happen when I touch Screen1.

If anyone could provide any help, guidance, point out errors, or even just give suggestions, that would be wonderful. I am still learning and only have a small general understanding of lua. Anything that you can share would be a huge help!

:purple_heart: With love,
Mono

LocalScripts do not run in the Workspace. Consider moving it to StarterPlayerScripts or transforming it into a Script.

Also, the extra end at the end of each script is unnecessary. Remove those.

Second also, you should combine both scripts into one, and instead of having two different screens, have one and just change the image ID of it when it is touched. Script example:

local screen = script.Parent.Parent -- The screen
local decal = script.Parent -- The decal

local blueScreenId = 000000 -- The ID of the bluescreen image

screen.Touched:Connect(function()
    -- Converts the ID into an image URL string
	decal.Texture = "rbxassetid://" .. tostring(blueScreenId)
end)
1 Like

Here is how I would handle this problem:

  1. First, I would add a ‘hitbox’ on the laptop, this is basically a big block around the laptop which is transparent and CanCollide set to false. This is because it’d be hard to touch the screen as a player (you would have to jump onto it), but with the hitbox it’d be easier to touch.

  2. I would create a function to handle switching the screens for me.

Firstly, I recreated your Screens folder, but I put the script directly under the folder. I used a server script (normal script) as apposed to a local script as I wanted the change to show for everyone.

My recreation
image

local screenFolder = script.Parent
local directionBool = false

function toggleScreens()

	if directionBool == false then
		directionBool = true

		screenFolder.Screen1.Transparency = 1
		screenFolder.Screen1.Decal1.Transparency = 1

		screenFolder.Screen2.Transparency = 0
		screenFolder.Screen2.Decal2.Transparency = 0

	else
		directionBool = false

		screenFolder.Screen1.Transparency = 0
		screenFolder.Screen1.Decal1.Transparency = 0

		screenFolder.Screen2.Transparency = 1
		screenFolder.Screen2.Decal2.Transparency = 1
	end

end

screenFolder is a variable that directly references the screen folder.

directionBool is a boolean value (true OR false) which I use to store the current state of the screens. If the value is false, then it makes screen 1 invisible, and screen 2 visible, but if the value is true, it makes screen 1 visible and screen 2 invisible.

directionBool = not directionBool 

This line over here toggles the bool at the end of the function.

Now that we’ve created the function, I would test it before moving on.

I’ll use this script to test it; it’s a simple loop.

while task.wait(2.5) do
	toggleScreens()
end

This is a fairly simple loop, it just executes everything inside it every 2.5 seconds

testSuccess

Success! Now it’s time to set up the touched event.

First, I’ll create the hitbox.

There we go, I’ll call it hitbox and put it inside the screens folder.
image

Now, it’s time to script the touched event.
image

This is nice and simple, however, you’ll notice there’s a difference between my touch event and yours, and that is the “hit” between the brackets. Why? It’s because the touched events actually tells you which block touched it. How were you supposed to know this? Well, that’s why we have the API documentation, basically a website of every single thing inside the roblox lua language. You can find it here, and the documentation for the touched event here.

Okay, so now we have the touched event, and the event will tell us what triggered the touched event. But, we don’t want everything to trigger the touched event, just players. So we can add in some lines to make sure, 1. what touched it was a player, 2. the player isn’t death. I’ll do this now;

hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
		
	end
end)

In case you didn’t know, every character has a humanoid, and a humanoid object has a health property. Here, using :FindFirstChild, we check if the parent of what hit the hitbox has a humanoid, and if that humanoid has more health than 0, it carries on.

Now we can add in our function call.

hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
		toggleScreens()
	end
end)

Let’s test it.
testSuccess2

It works! However, you might notice that it kinda spams, that’s because when you touch a block, the touched event doesn’t just trigger once, it triggers multiple times. To fix this, we need a cooldown bool. Let’s add this at the top of the script:

local screenFolder = script.Parent
local hitbox = screenFolder.hitbox


local directionBool = false
local cooldownBool = false

Now let’s implement the bool into our script.

hitbox.Touched:Connect(function(hit)
	if cooldownBool == false then
		cooldownBool = true
		
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
			toggleScreens()
		end
		
		task.wait(1)
		cooldownBool = false
	end
end)

Time to test again.

testSuccess3

And everything works!

I assume you already know how if statements, functions and loops work. If you don’t, I recommend you look up a baisc roblox lua tutorial.

Hoped this helped, let me know if you run into any issues.
tay_z3r

1 Like

My final script ended up looking like this:

local screenFolder = script.Parent
local hitbox = screenFolder.hitbox


local directionBool = false
local cooldownBool = false


function toggleScreens()

	if directionBool == false then
		directionBool = true

		screenFolder.Screen1.Transparency = 1
		screenFolder.Screen1.Decal1.Transparency = 1

		screenFolder.Screen2.Transparency = 0
		screenFolder.Screen2.Decal2.Transparency = 0

	else
		directionBool = false

		screenFolder.Screen1.Transparency = 0
		screenFolder.Screen1.Decal1.Transparency = 0

		screenFolder.Screen2.Transparency = 1
		screenFolder.Screen2.Decal2.Transparency = 1
	end

end

hitbox.Touched:Connect(function(hit)
	if cooldownBool == false then
		cooldownBool = true
		
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
			toggleScreens()
		end
		
		task.wait(1)
		cooldownBool = false
	end
end)
1 Like