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

Why does my script not work after the event is fired?

I was trying to make a prison door involving the use of a ClickDetector which toggles the prison door. I used a LocalScript which fired a RemoteEvent when the ClickDetector was clicked, then my script detects when the event is fired and should of made the door change colors.

below is my LocalScript:

local RPS = game:GetService("ReplicatedStorage") -- fires the event for JailCell1
game.Workspace.JailCell1.cardVerify.ClickDetector.MouseClick:Connect(function(char)
	local character = char.Parent
	local doorName = 1
	RPS.Modules.Events.triggerDoor:FireServer(character, doorName)
end)
game.Workspace.JailCell2.cardVerify.ClickDetector.MouseClick:Connect(function(char)
	local character = char.Parent -- fires the event for JailCell2
	local doorName = 2
	RPS.Modules.Events.triggerDoor:FireServer(character, doorName)
end)

below is my ServerScript:

--jail cells--
local RPS = game:GetService("ReplicatedStorage")
RPS.Modules.Events.triggerDoor.OnServerEvent:Connect(function(player, character, doorName)
	local player = game.Players:GetPlayerFromCharacter(player)
	if doorName == 1 then
		local jailCell1 = workspace.JailCell1
		if jailCell1.Union.Transparency == 0 then
			jailCell1.Union.Transparency = 1
			jailCell1.Part.CanCollide = false
			jailCell1.cardVerify.BrickColor = BrickColor.new("Dark stone grey")
		elseif jailCell1.Union.Transparency == 1 then
			jailCell1.Union.Transparency = 0
			jailCell1.Part.CanCollide = true 
			jailCell1.cardVerify.BrickColor = BrickColor.new("Medium stone grey")
		end
	
	end
	if doorName == 2 then
		local jailCell2 = workspace.JailCell2
		if jailCell2.Union.Transparency == 0 then
			jailCell2.Union.Transparency = 1
			jailCell2.Part.CanCollide = false
			jailCell2.cardVerify.BrickColor = BrickColor.new("Dark stone grey")
		elseif jailCell2.Union.Transparency == 1 then
			jailCell2.Union.Transparency = 0
			jailCell2.Part.CanCollide = true 
			jailCell2.cardVerify.BrickColor = BrickColor.new("Medium stone grey")
		end
	end
end)

I have had trouble with all of my RemoteEvents simply doing nothing, the event is fired but nothing else happens. No errors are given. Thanks for reading!

1 answer

If you want to change something with a ClickDetector for the whole server, you do not need either LocalScripts or RemoteEvents. You have a part, which is your door, inside of it, put a ClickDetector and a Server Script. In the Server script write this:

--Variables
local Door = script.Parent
local ClickDetector = script.Parent:WaitForChild('ClickDetector')

--Code
ClickDetector.MouseClick:Connect(function(plr) --gets **player who clicked**, so a player object
	
	Door.BrickColor = BrickColor.new('Dark stone grey')
	--or some other code
end)

If you ever need to get a character from clickdetector, here's a simple server script

local ClickDetector = script.Parent:WaitForChild('ClickDetector')

ClickDetector.MouseClick:Connect(function(plr) --Getting player object of player who clicked the clickdetector
	
	local char = plr.Character --Defining player's character
	
	if char ~= nil then -- If we find character
		
		print(6) --Do stuff
	
	else --If we dont
		
		print(3) --Do other stuff
			
	end
	
end)

That's it. I'm sorry for the late reply and if I made a mistake please correct me.

2 comments

0

Thank you for answering! The reason why I used a RemoteEvent was because I heard that ClickDetector function arguments didn't have script.Parent:FindFirstChild("Humanoid") to be able to be used.. Thanks!

0

You probably heard because it's true. Clickdetectors arguments get the player who clicked, so a player object. If you need a character from a clickdetector, i've edited my answer.

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