Why is this script working but mine isnt?

Trying to locally change a part’s transparency when a key is pressed but it doesn’t work. I found that all my code is working except for the actual change in transparency. I found a similar topic, and i copied the script that the solution supplied and it worked, but when I try to change the transparency nothing happens, I cant tell what the other script is doing, both of them are in starter player scripts.

Script from the topic i found, modified

 local part_to_modify = workspace:WaitForChild("Baseplate")
 

 local function modify(part)
  	   part.Transparency = 1
       wait(.1)
       part.Transparency = 0
	   wait(.1)
	   part.Reflectance = 1
	   wait(.1)
   part.Reflectance = 0

 end

wait(10)
print("Modifying death barrier.")
modify(part_to_modify)

Script im making

Transparency = 0
DeathBarrier = workspace.Baseplate
Transparency = DeathBarrier.Transparency

local PlayerService = game:GetService('Players');
local UserInputService = game:GetService('UserInputService');
---------------------------------------------------------------------
UserInputService.InputBegan:Connect(function(keyPressed,currentlyOccupied)
	if currentlyOccupied then return end
	print (keyPressed.KeyCode)
	
	if keyPressed.UserInputType == Enum.UserInputType.Keyboard then
		if keyPressed.KeyCode == Enum.KeyCode.E then
			print (Transparency)
			Transparency = 0
		end
	end
end)
1 Like

Are you using this code in a Server or Local script? UserInputService only works in the Local scripts. I ran your code in a Local Script under StarterPlayerScripts and the code works as intended.

yes why would i use a server script

The only issue I could see in your code is you are not giving the script time to load the workspace before running the code. Try using this:

DeathBarrier = workspace:WaitForChild("Baseplate")

Instead of:

DeathBarrier = workspace.Baseplate

When I ran the code in a new studio file it ran as expected.

Which script? The first or second one?

The second one that is your script.

i didnt get any errors unless im mistaking it for some other error.
the rest of the script wouldnt run anyway, im still getting outputs from me pressing keys.

I was unaware that you were using both scripts. I thought the first script was the script you found and the bottom was the edited version. I will reevaluate this.

i am not using both scripts, im just trying to find out whats wrong by comparing the 2.

Transparency = 0
DeathBarrier = workspace.Baseplate
Transparency = DeathBarrier.Transparency

Here you defined Transparency twice.

Additionally your Variable is the baseplate transparency number. Meaning your your just changing the variable in the script and not changing the actual property

1 Like

am i supposed to do something like Transparency.Value?

You need your variable to call the object than change the property:

DeathBarrier = workspace.Baseplate
DeathBarrier.Transparency = 0
1 Like

OK this has been fixed, thank you.