Part that sends message when touched only works the first time

  1. What do you want to achieve? I want to make a part, that, when touched, sends a message.

  2. What is the issue? The issue is that it does work the first time, but it doesn’t after the first time. I also get no errors.

  3. What solutions have you tried so far? I tried to look for solutions on the Developer forums, but I can’t seem to find a solution.

Here is my code (Server Sided):

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the script handling the touch events. Before you ask, I have hooked up the event onto a LocalScript.

    local Players = game:GetService("Players")
    local remoteEvent = game.ReplicatedStorage:WaitForChild("WinTower")
    local connection
    connection = script.Parent.Touched:Connect(Touch)
    local Teleport = "WinPad Teleporter Target"
    function Touch(hit) 
    	local player = Players:GetPlayerFromCharacter(hit.Parent)
    	if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then 
    		script.Parent.Locked = true
    		script.Parent.Parent:findFirstChild(Teleport).Locked = true --Checks Debounce.
    		local player = Players:GetPlayerFromCharacter(hit.Parent)
    		player.Team = game:GetService("Teams").Winners
    		player.Win.Value = true
    		player.InTower:Destroy()
    		game.ReplicatedStorage.WinTower:FireClient(player, script.Parent.TowerName.Value)
    		local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
    		hit.Parent:moveTo(Pos.Position) 
    		wait(1) 
    		script.Parent.Locked = false 
    		script.Parent.Parent:findFirstChild(Teleport).Locked = false 
    	end 
    end 
    script.Parent.Touched:connect(Touch)

Can someone point out where I have done this wrong?
Thank you!

1 Like

Connected the Touch event to a function way too early, it seems. You must connect it right after the function, not before the function. Atleast this is what I can see from your script.

1 Like

Still, this only works for the first time.

Oh, by the way, I do get teleported.

Odd. I tried your code out and just erased some of the lines. It works everytime, not just once. There might be another script that is locking your two parts besides your own script locking and unlocking it, although I may be wrong.

Erased which lines? Can you send me your code? Thanks!
Also, there is no other script changing the Locked property.

local Players = game:GetService("Players")
--local remoteEvent = game.ReplicatedStorage:WaitForChild("WinTower")
local connection
local Teleport = "WinPad Teleporter Target"
function Touch(hit) 
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then 
		script.Parent.Locked = true
		script.Parent.Parent:findFirstChild(Teleport).Locked = true --Checks Debounce.
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		--player.Team = game:GetService("Teams").Winners
		--player.Win.Value = true
		--player.InTower:Destroy()
		--game.ReplicatedStorage.WinTower:FireClient(player, script.Parent.TowerName.Value)
		local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
		hit.Parent:moveTo(Pos.Position) -- Where the error occurs because it's getting my accessory
		wait(1) 
		script.Parent.Locked = false 
		script.Parent.Parent:findFirstChild(Teleport).Locked = false 
	end 
end 
connection = script.Parent.Touched:connect(Touch)

This is the code. I copy and pasted your code. The only problem I ran into that sent an error into my output was:

"moveTo is not a valid member of Accessory “Workspace.GhostShinjiro.MeshPartAccessory” - Line 17

I simply just removed my hats and it works fine. You may need to get a part of your character properly instead of an accessory or anything else so it doesn’t error.

Oh and the comments are the ones I “removed”.

Oh. Mine does teleport them, and I don’t get any accessory errors. However, the event doesn’t fire every time.
Maybe, can you try hooking up the event?

Which event specifically? The remote event?

Also, I think I misunderstood your problem the first time. The issue you’re getting is because of the remote event, yes?

Yes. The remote event is the part that doesn’t fire, with no errors.

Alright, I made it. The remote event fires everytime. It has something to do with your local script for your remote event then. May you show me your local script? You may as well tell me what the script and the local script is supposed to do.

Sure!
LocalScript in StarterGUI → Timer → TimeScript

local StartTime
local TSeconds = 0
local Minutes = 0
local Seconds = 0
local Mseconds = 0
local Started = false
local GL
local TS=game:GetService'TweenService'
function game.ReplicatedStorage.RequestTime.OnClientInvoke()
	return TSeconds,Minutes,Seconds,Mseconds
end
function game.ReplicatedStorage.RequestToolInfo.OnClientInvoke()
	return GL
end
function _G:ToggleRestartLabel(v)
	local info=TweenInfo.new(v and _G.QuickResetDelay or 3 or .5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
	local tw=TS:Create(script.Parent.Restart,info,{TextTransparency=v and 0 or 1})
	tw:Play()
end
game.Players.LocalPlayer.ChildAdded:Connect(function(Object)
	if Object.Name == "InTower" then
		if Started ~= true then
		Started = true
		GL = {}
		StartTime = workspace.DistributedGameTime
		local TowerName = game.Players.LocalPlayer:WaitForChild("InTower").Value
		repeat
			wait()
			local findgear = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Tool")
			if findgear then
				--if findgear.Name == "Vertical Mobility" or findgear.Name == "Bootleg Coil" then
					--for index,value in pairs(GL) do
					--	if index == findgear.Name then
					--		print(GL[index])
							GL[findgear.Name] = true
					--	end
					--end
				--end
			end
			TSeconds=math.floor(workspace.DistributedGameTime-StartTime)
			Minutes = math.floor(math.floor(workspace.DistributedGameTime-StartTime)/(60*1))
			Seconds = math.floor(math.floor(workspace.DistributedGameTime-StartTime)/(1))%(60)
			Mseconds = math.floor((workspace.DistributedGameTime-StartTime)*(100))%(100)
			script.Parent.TextLabel.Text = string.format('%s:%02i.%02i',Minutes,Seconds,Mseconds)
		until game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Health <= 0 or game.Players.LocalPlayer:FindFirstChild("InTower") == nil
			local Win = game.Players.LocalPlayer.Win.Value
			wait(1)
			script.Parent.TextLabel.Text = "0:00.00"
			Started = false
			game.ReplicatedStorage:WaitForChild("WinTower").OnClientEvent:Connect(function(name)
				game.ReplicatedStorage:WaitForChild("WinTowerTime"):FireServer(TowerName, Minutes,Seconds,Mseconds)
			end)
		end
	end
end)

ServerScript in ServerScriptService

game.ReplicatedStorage.WinTowerTime.OnServerEvent:Connect(function(player, name, minutes, seconds, mseconds)
	local color
	local colors = {TowerofAnnoyingObstacles = BrickColor.new("Bright red")}
	color = colors[name:gsub(" ","")]
	local time_string = string.format('%s:%02i.%02i', minutes, seconds, mseconds)
	local message = "" .. player.Name .. " has beaten " .. name .. " in " .. time_string .. "!"
	game.ReplicatedStorage.WinMessage:FireAllClients(color, message)
end)

LocalScript in StarterPlayer → StarterPlayerScripts

local remoteEvent = game.ReplicatedStorage:WaitForChild("WinMessage")
remoteEvent.OnClientEvent:Connect(function(color, message)
	local b = color
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = message;
		Font = Enum.Font;
		Color = b.Color;
		FontSize = Enum.FontSize.Size96;
	})
end)

Apologies for the late reply.

Honestly, everything seems to be in order in my viewpoint. I don’t know how it only happens once. Sorry. I have thought of everything I could. :frowning:

try doing

while true do
    function Touch(hit) 
    	local player = Players:GetPlayerFromCharacter(hit.Parent)
    	if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then 
    		script.Parent.Locked = true
    		script.Parent.Parent:findFirstChild(Teleport).Locked = true --Checks Debounce.
    		local player = Players:GetPlayerFromCharacter(hit.Parent)
    		player.Team = game:GetService("Teams").Winners
    		player.Win.Value = true
    		player.InTower:Destroy()
    		game.ReplicatedStorage.WinTower:FireClient(player, script.Parent.TowerName.Value)
    		  local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
    		    hit.Parent:moveTo(Pos.Position) 
    		    wait(1) 
    		    script.Parent.Locked = false 
    	     	script.Parent.Parent:findFirstChild(Teleport).Locked = false 
            end
    	end 
    end

    script.Parent.Touched:connect(Touch)
1 Like

By the way, is “Locked” a BoolValue? Because you’re doing script.Parent.Locked == false , but if it’s an instance, it should be script.Parent.Locked.Value == false . Same goes for script.Parent.Parent:FindFirstChild(Teleport).Locked == false which should have have a .Value after .Locked.

1 Like

It does set it correctly, so I’m assuming it doesn’t need a .Value after it.

Alright! It is completely fine.

1 Like

I have figured it out!
This is the script now:

local Players = game:GetService("Players")
local remoteEvent = game.ReplicatedStorage:WaitForChild("WinTower")
local connection
local Teleport = "WinPad Teleporter Target"
local sentMessage = false
function Touch(hit) 
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not script.Parent.Locked and not script.Parent.Parent:findFirstChild(Teleport).Locked then 
		script.Parent.Locked = true
		script.Parent.Parent:findFirstChild(Teleport).Locked = true
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		player.Team = game:GetService("Teams").Winners
		player.Win.Value = true
		player.InTower:Destroy()
		
		local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
		hit.Parent:moveTo(Pos.Position) 
		wait(1)
		script.Parent.Locked = false 
		script.Parent.Parent:findFirstChild(Teleport).Locked = false
		wait(0.1)
		game.ReplicatedStorage.WinTower:FireClient(player, script.Parent.TowerName.Value)
	end
	
end --Takes you there and Ends the Function.
script.Parent.Touched:connect(Touch) --Listens out for Touchers.

Thanks for your help, everyone!

2 Likes