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?
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!
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.