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

Can I get some help troubleshooting my script?

I'm puzzled. I want it to clone the GUI for each player, and whatnot. It works, but it clones it a bunch of times, and they dont count-down the whole time I put in. Can someone tell me why?

print("enter")
function countdown(length)
	for k,p in pairs (game.Players:GetPlayers()) do
    local h=p.PlayerGui.TimerGui.Frame.TextLabel
    for i = length,1,-1 do
        h.Text = tostring(i)
				
				
			
		
	end
end
       
    
   
end

print("entrrhdher")
debounce=false
while true do
	wait()
for k,plr in pairs(game.Players:GetPlayers()) do
	
	if plr.Name=="Obscuriti" or plr.Name=="MathisGuidonsio" or plr.Name=="Tempestatem" or plr.Name=="ImperatorVince" then
		
    plr.Chatted:connect(function(msg)

	
	
        if msg:sub(1,7) == "/timer " then
	
	for k, p in pairs(game.Players:GetPlayers()) do
		print("Cloning")
	m=game.Lighting.TimerGui:Clone()
	m.Parent=p.PlayerGui
	end
            local time = tonumber(msg:sub(8)) 
            countdown(time)
	for k, p in pairs(game.Players:GetPlayers()) do
	p.PlayerGui.TimerGui:Destroy()
	end		
	
elseif msg:sub(1,9)== "/timerend" then
	for k,p in pairs (game.Players:GetPlayers()) do
		p.PlayerGui.TimerGui:Destroy()
	end
	
		
	
	
end
      
	
end)
	end
end
end

2 answers

Three things I can see: msg:sub(), plr.Name == "LolPerson", and 'if' statements. When I do this kind of coding, I like to use the ':lower()' string so that the commands aren't Cap-Sensitive. Also, main I use the ':sub()'s for when its like '/kill player', I'd do the commands like this;

if msg:lower():sub(1,7) == "/timer " then --Not cap-sensitive anymore
for i,v in pairs(game.Players:GetPlayers()) do --Gets all players
if v.Name:lower() == msg:sub(8) then --If name matches to the name that the speaker said then
if game.Lighting:FindFirstChild("TimerGui")and v and v:FindFirstChild("PlayerGui") then --I use this to see if the things exist, elseif we don't, the script may error :l
m=game.Lighting.TimerGui:Clone() --Clones the GUI
m.Parent=p.PlayerGui --Parents the Cloned gui into the Player's PlayerGui
end --The end for the 'if' statement that checks for the things
end --The end for the Player Name matching
end --The end for the 'for' loop (Get it?)
end --The end for the 'if msg:lower():sub() == "nil" then' statement

Now, the name Checker, I'd probly do this;

if msg.Name:lower() == ("PLAYER"):lower() then --I do this just incase when the player joins, it'll lowercase all (Non-Cap-Sensitive stuff again), instead of erroring things that 'LOL' and 'lol' don't match because one is all lowercase and the other is all uppercase
end --The end for this 'if' statement

Last but not least, the 'if' statements for the 'msg', I know this might be a bit harder, but its just my way without elseif's;

if msg:lower() == "nil" then --If the player chatted 'nil' then
print("nil is a keyword") --Will print 'nil is a keyword'
end --The end for the 'if' statement

if msg:lower():sub(1,4) == "nil/" --If command starts with 'nil/' then
print(msg:sub(5).." is nil") --Will print what the player said after 'nil/', and also do ' is nil' after what the player said
end

I like to do that kind of thing for seperate commands, especialy Admin Commands. Now, I just noticed you were using a 'while true do end' loop for it, no no no you Silly Billy, try using the PlayerAdded event for it;

game.Players.PlayerAdded:connect(function(plr) --Player has joined
if plr.Name:lower() == ("PLAYER"):lower() then --Does plr.Name equal to PLAYER (Non-Cap-sensitive)?! :o
plr.Chatted:connect(function(msg) --He does! He gets to use Chatted commands! :D

-- Rest of coding

end) --The end for the .Chatted event
end --The end for the 'if' statement
end) --The end for the PlayerAdded event

Also, your GUI may not be working due to the recent GUI update that which Roblox broke. :l I hope this helped bro!

You could try debouncing, or maybe having the script check to see if there are already GUI's in the player, and to remove them if they're already there. Multiple clones of something can cause problems with the script.

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