LocalScript randomly not working

For some reason, a localscript in a part in which touched makes a gui appear, sometimes does not seem to succeed. It seems to have a random chance between executing the action and failing to do so. The script is below:

local TS = game:GetService("TweenService")

script.Parent.Touched:Connect(function(hit)
	print("fired")
	script.Parent.CanTouch = false
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player.PlayerGui:FindFirstChild("TeleportScreen") then else
			game.ReplicatedStorage.paint:FireServer()
			player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
			print("found")
			game.SoundService.painting:Play()
			local clone = script.TeleportScreen:Clone()
			clone.Parent = player.PlayerGui
			clone.Enabled = true
			TS:Create(clone.TextLabel, TweenInfo.new(1), {BackgroundTransparency = 0, TextTransparency = 0}):Play()
		end
		else print ("failed")
	end
end)

As you can imagine, the console will print “failed” on the times it does not succeed. Is there a solution to this?

You can’t have a local script in the workspace. You’d have to make this either a server script or adapt your local script under the StarterPlayer/CharacterScripts or StarterGui

this should be a server script and use a Remote Event to trigger the tween service

A workaround is creating a Server Script, copy/pasting the code from the snippet provided into the script, and set the script’s RunContext to Client.

However be aware that you WILL need to add sanity checks (WaitForChild, FindFirstChild)

It’s not the best workaround but works for what you need to do. I don’t recommend doing this all the time though, you should do what @IrishStukov suggested instead.

Hope your M64 project goes well :slight_smile:

I put it in StarterPlayerScripts, and it still seems to not work some of the time.
I tested other scripts that basically have the same functionality, and this is the only one which behaves as it does.

You have to reference the part and replace script.Parent

StarterPlayerScripts will kinda work, but it’s not recommanded. put it in startercharacterscripts (it will run everytime your character respawns. If you want it to run only once for each player, put it in starterplayerscripts. If you want it to run only once each game, make it serversided) and replace the unnecessary code with this:

local TS = game:GetService("TweenService")
local players = game:GetService("Players")
local localPlayer = players.localplayer
local playerGui = localPlayer.PlayerGui
local part = workspace.part -- replace with your part path
local storage = game.ReplicatedStorage

part.Touched:Once(function(hit)
	print("fired")

	local char: Model? = hit:FindFirstAnsestorOfClass("Model")

	if char and players:GetPlayerFromCharacter(char) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player == localPlayer and not playerGui:FindFirstChild("TeleportScreen") then
			storage.paint:FireServer()
			char:FindFirstChild("HumanoidRootPart").Anchored = true
			print("found")
			game.SoundService.painting:Play()
			local clone = script.TeleportScreen:Clone()
			clone.Parent = playerGui
			clone.Enabled = true
			TS:Create(clone.TextLabel, TweenInfo.new(1), {BackgroundTransparency = 0, TextTransparency = 0}):Play()
		end
	else 
		print ("failed")
	end
end)

You disabled cantouch when you didnt have to, added unnecessary end lines etc.
Because this is a localscript you only have to keep track of yourself not other players.

The problem now is that since it will only fire the touched event once, if it touches anything other than a character, it will fail. To prevent that replace :Once(...) with :Connect(...) and if you want to disconnect it later, make the connection a variable: local connection = part.Touched:Connect(...) then in or out of the function, you can call connection:Disconnect() to stop firing the event.

If there are more parts like this you can tag them a specific tag and change the part variable to: local parts = game.CollectionService:GetTagged("your tag") then do:

for _, part in parts do
	part.Touched:Connect(function(hit)
		--logic
	end)
end

This approach will 99.9% work, let me know if you want a serversided script guide whch is kind of unnecessary though since it will cause ping issue

cuz another part is first touching it and its parent is not a player character causing it to turn cantouch off

Even with this revised script in in StarterCharacterScripts, the script sometimes prints “failed”, albiet more rarely. I had changed the “Once” to “Connect” as well

Which can only mean one thing; something else is touching it.
Just disconnect it only when the player touches, ignore if it’s anything else like a part or smth.