Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with cloning and destroy?

i'm trying to make an spectate script that only works on lobby so i put a script on the floor of the lobby that clone one object named inlobby to the player character but it only works in solo mode it doesn't work in the server mode and on the roblox player

here is the script (i made the if cuz when i made it without it, he cloned the object infinitely) ( im using a normal script not a local script)

script.Parent.Touched:connect(function(hit)
if game.Players.LocalPlayer.Character:FindFirstChild("inlobby")then
else
game.Lighting["inlobby"]:clone().Parent = game.Players.LocalPlayer.Character
end
end)

and i made an delete script too (when a player teleports to the match the game deletes the inlobby object) but it wont works too. (normal script too)

local inlobby = game.Players.LocalPlayer.Character.inlobby
				if game.Players.LocalPlayer.Character:FindFirstChild("inlobby")then
				inlobby:destroy()
                else
				end

3 answers

✓ Accepted answer

t0ny makes good points. Note that the for loop t0ny suggests is good to replace your second script, but for the first you should just use:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.Character and not player.Character:FindFirstChild("inlobby") then
	game.Lighting["inlobby"]:clone().Parent = player.Character
end

Also, the order you perform your checks doesn't work:

local inlobby = game.Players.LocalPlayer.Character.inlobby
if game.Players.LocalPlayer.Character:FindFirstChild("inlobby")then
	inlobby:destroy()
else
end

You access inlobby without using FindFirstChild first, meaning that if inlobby doesn't exist, it will cause an error. Also, the else is unnecessary since you don't do anything in that case. Do this instead:

local character = game.Players.LocalPlayer.Character
local inlobby = character and character:FindFirstChild("inlobby")
if inlobby then
	inlobby:Destroy()
end

(The character code is just there to make sure that it doesn't bug if Character happens to be nil). As t0ny implied, this script will only work if it's a LocalScript (as only LocalScripts have access to LocalPlayer). If you're using a script, use t0ny's for loop (using the player variable instead of game.Players.LocalPlayer).

1 comment

0

Totally jacked my accepted answer :( And I lost 1 rep xD. Anywhos, thanks for the credit tho. And its always nice to see people go indepth on answers. I kinda lazed out a bit.

Why are you storing things in lighting? Use ReplicatedStorage or ServerStorage?

Why are you using game.Players.LocalPlayer in a regular script? It only works in local scripts.

You have to do a generic loop for regular scripts.

 for _, player in pairs(game.Players:GetChildren()) do
	-- code here
end

Also your doing a Touched event.. it passes whatever object it touched to the script.. therefore why not do hit.Parent ?? but make sure you check if its a humanoid first... Since its passing an object.. and the object touching it would be a player... check if its a player.. and if it is.. use it.

This discussion is preserved as it was. New answers, comments, and votes are closed.