Part cloned inside a LocalScript is not receiving Touched event

Hello there. I am having an issue where a part that I clone within a LocalScript is not receiving any Touched events.

local movingFish = fishModel:Clone()
movingFish.Parent = workspace.MovingFish

The following is the Script inside the part (note it is not a LocalScript)

local db = false
local players = game:GetService("Players")

local function onTouch(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local character = player.Character
			local humanoid = character:WaitForChild("Humanoid")
			humanoid.Health = 0
		end
	end
end

script.Parent.Touched:Connect(onTouch)

I’ve spent a good deal of time going through the forums on here but could not find a solution. Any insights would be greatly appreciated, thank you

The problem is that anything you do on the client does NOT replicate to the server. That means that the server script doesn’t register that you cloned the part, and moved it into game.Workspace. Your best bet is to convert the script into a local script, and move the newly-converted local script into a place where it works (as a local script only works in some places).

Or get the LocalScript to fire a Remote to do the cloning with an remote event listener server side.

Hi, thank you for your insight, it makes sense. You mentioned that “(as a local script only works in some places)”. Can you elaborate on this point please.

There’s something called the server-client boundary which allows clients and the server to communicate without security risks to other players.

LocalScripts run on the client, meaning the players computer. Scripts run on the server, which is the authority, it tells all of the connected clients what is true.

Since the server is the authority, a client doing something like, cloning a brick for example, will not be replicated to the server or other clients. (And server scripts cannot be run on the client)

So in this senario you’re telling a client’s computer to clone a brick which contains server code (that the client has no authority to run)

Sorry, I can elaborate more.

The Roblox Documentary says exactly this on the first line to describe what I mean: The client container services are meant for objects that are replicated to every connected client.

Basically, you have to place LocalScripts in certain places for it to replicated to each and every client. The server listens to every client container service for LocalScripts, which is how it interprets it. That is why you can’t just place it in a random part and expect it to work, as it isn’t a client container service, meaning the service wouldn’t interpret it.

Hope this helps! I also put client container services down below if it helps.

  1. StarterPack
  2. StarterGui
  3. StarterPlayerScripts
  4. StarterCharacterScripts
  5. Replicated Storage

I see, thank you for the explanation. I managed to rework my code and it’s behaving as intended now :grinning:

Glad I could help :wink: ! Good luck on your project!

Sorry to bother you, but since the code works, may you please mark my first post as a solution? This will just ensure that if anyone has the same problem, this post could help them as well. Thank you, and again, good luck on your project!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.