How to :Clone a model/part locally?

Good evening all!

I’m having trouble cloning a model/part locally on the client.

I’m using a localscript (but have also tried a serverscript with RunContext = Client) to show a part/model when a box is touched, and make it disappear when another is touched.

Unfortunately, the part/model appears globally, despite me needing to to only appear locally.

Heres the code:

local enter = game.Workspace.enter
local leave = game.Workspace.leave
local peter = game.ReplicatedStorage.PETER
local vis = false


enter.Touched:Connect(function()
	if vis == true then
		print("Already visible!")
	end
	if vis == false then
		NPC = game.ReplicatedStorage.PETER:Clone()
		NPC.Parent = game.Workspace
		vis = true
	end	
end)

leave.Touched:Connect(function()
	if vis == true then
		NPC:Destroy()
		vis = false
	end
	if vis == false then
		print("Nothing happened!")
	end
end)

(The part is called peter because it is a picture of peter griffin for testing purposes)

Any help is appreciated! Is there a way to get this to work?

Thanks,

innit_2winnit :slight_smile:

2 Likes

Have you checked server view? Maybe all client recieve touched events from all players.

2 Likes

I tested it using an alt on my phone. When one account steps into the box, the model appears, even for the account that stood still.

1 Like

Touched events will fire if any thing touches it. Even if you have it locally connected → is player1 were to step on the part → Player2’s touched connection would still fire cause a part did in fact touch it. All you have to do is check if the player that touched the part is == to the local player.

For this we have to use the value returned by a .Touched connection. Which is the BasePart that touched it.

E.g for when they enter:

local PlayerService = game:GetService("Players");
local LocalPlayer = PlayerService.LocalPlayer;

enter.Touched:Connect(function(Hit)
	local GetHitPlayer = PlayerService:GetPlayerFromCharacter(Hit.Parent);
	
	if GetHitPlayer and GetHitPlayer == LocalPlayer then
		print("this means that our client touched the part!!")
		-- do ur peter griffen stuff here
	end;
end);

and you would do the exact same thing for when they leave so that other players don’t make peter disappear!

2 Likes

Make a local script in StarterGui that has the scripts that clone the model, make sure the model is in ReplicatedStorage. Just so you know, I also encountered this issue and attacked it easily.

1 Like

Hey! Sorry for the late reply, I was super busy :slight_smile:

I tried this out in studio and it works brilliantly! Thank you for your help, I really appreciate it!

I will mark your answer as the solution.

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