Why is my block not firing its function?

I am trying to make a block that once it is touched, makes a character change their team and clones a frame which shows the player a message. My issue is that when I touch this block, nothing happens. My player is detected, but nothing else seems to work.

script.Parent.TriggerUnemploymentCafe.Touched:Connect(function(hit)	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local character = humanoid.Parent
		local localPlayer = Players:GetPlayerFromCharacter(character)
	localPlayer.Team = TeamService.PoliceOfficer
	if CheckTeam(localPlayer) == TeamService.CafeWorker
		then 
		if localPlayer.PlayerGui.gameResponse:FindFirstChild("WarnUnemployed") then return end 
	else	
	local UnemployedMessage = localPlayer.PlayerGui.gameResponse.WarnCharacter:Clone()
	 	UnemployedMessage.Title.Text = "You are now unemployed because you left the area. Return to the cash register to keep your job."
		UnemployedMessage.ContinueButton.MouseButton1Down:Connect(function()
		UnemployedMessage:Destroy()
		UnemployedMessage.Name = "WarnUnemployed"
		end)
		end
end	
end)
1 Like

Please format your code bettter, it makes it 100 times harder for anyone to debug it since the formatting is way off.

Does the output display anything?

No, it doesn’t. I did lua

which I was told to do, doesn’t seem to be working,

I believe @12904 is referring to your indentation levels. Take a look at this topic I put up about formatting your code when requesting support, primarily the indentation one.

What do you expect to happen with your code? You say that your player is detected but how do you know this exactly.

Scripts can’t access PlayerGui. You have to do this through a local script by using a remote event.

1 Like

I reformatted the code while trying to dissect the problem. I hope that helps.

script.Parent.TriggerUnemploymentCafe.Touched:Connect(function(hit)	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local character = humanoid.Parent
		local localPlayer = Players:GetPlayerFromCharacter(character)
		localPlayer.Team = TeamService.PoliceOfficer
		if localPlayer.Team == TeamService.CafeWorker then 
			if localPlayer.PlayerGui.gameResponse:FindFirstChild("WarnUnemployed") then return end 
		else	
			local UnemployedMessage = localPlayer.PlayerGui.gameResponse.WarnCharacter:Clone()
	 		UnemployedMessage.Title.Text = "You are now unemployed because you left the area. "..
				"Return to the cash register to keep your job."

			UnemployedMessage.ContinueButton.MouseButton1Down:Connect(function()
				UnemployedMessage:Destroy()
				UnemployedMessage.Name = "WarnUnemployed"
			end)
		end
	end	
end)

I printed the LocalPlayers userId with success, and I also printed that the humanoid was found after the line local humanoid = hit.Parent:FindFirstChild("Humanoid")

Thank you for that, I’m absolutely terrible at code organization

So I have a question, what does CheckTeam do? You could easily just index the team from localPlayer using localPlayer.Team.

it does the same thing as localPlayer.Team, was just testing out that.

1 Like

Yes they can. The only difference is that client changes to Guis and the PlayerGui container do not replicate to the server and it’s bad practice to handle Guis from the server for anything other than cloning Guis there.

1 Like

You never parented the Unemployed message. Your clone has no parent

I parented it before, it should just clone it with the same parent that the item being cloned is in.

Nope, if you call :Clone() on an instance it has no parent unless you set it.

On the same theme, if the original that you’re cloning is not visible, you’ll need to set .Visible = true if you want to see it.

Otherwise you’ve got a cloned frame with no parent that isn’t visible.

1 Like

Ok, I made the script fire an event to change the playerGui. I am now realizing that the team is not detected, an issue I constantly have while scripting. What should I do? I am sure that my players team is CafeWorker, it prints as cafe worker and another part when touched makes my players team cafeworker.

What do you mean by the team is not detected? Is TeamService == game:GetService("Teams")? If you’re looking to find the player’s team, use localPlayer.Team as shown in @goldenstein64’s message.

I used this code if localPlayer.Team == TeamService.CafeWorker then print(" you are in the team") and had no output

nevermind, just received output but now i have the issue of my remoteevent being fired yet nothing happens… I literally can’t get remoteevents to work no matter how hard I try

Show your code. It makes it hard to help you without any context.

Here is my ServerScript:


script.Parent.TriggerUnemploymentCafe.Touched:Connect(function(hit)	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
	local character = humanoid.Parent
	local localPlayer = Players:GetPlayerFromCharacter(character)
	if localPlayer.Team == TeamService.CafeWorker then 
	local changeTextGui = Instance.new("RemoteEvent")
	changeTextGui.Parent = game.ReplicatedStorage.Modules.Events
	changeTextGui.Name = "changeText"
	changeTextGui:FireClient(localPlayer)	
		end	
	end
end)

Here is my localscript:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local localPlayer = Players.LocalPlayer
local changeTextGui = ReplicatedStorage.Modules.Events
local playerGui = localPlayer:WaitForChild("PlayerGui")

local warnUnemployed = playerGui.gameResponse.WarnCharacter:Clone()
warnUnemployed.Title = "You are now unemployed because you left the area. Return to the cash register to keep your job."
warnUnemployed.Parent = playerGui.gameResponse
warnUnemployed.ContinueButton.MouseButton1Down:Connect(function()
	warnUnemployed:Destroy()
end)

local function warningVisible()
	warnUnemployed.Visible = true
end

changeTextGui.OnClientEvent:Connect(warningVisible)```