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

Why does this script only run one time?

In a script, I have this:


workspace.k.OnServerEvent:connect(function(p)
	if script.Parent.Parent.selected.Value == true then
local base = game.Players:WaitForChild("Player1").Character["Right Arm"]
local han = script.Parent.Handle
han.Anchored = true
local w = Instance.new("Weld", han)
w.Part0 = han
w.Part1 = base
w.C0 = CFrame.new(0,1,0)
han.Anchored = false
script.Parent.Parent = p.Character
while true do
	wait(0.2)
	if p.PlayerGui.ScreenGui.one.selected.Value == false then
		p.Character:FindFirstChild("Model"):Destroy()
		break
	end
end
end
end)

That's what I want to run more than once.

In a localscript, I have this:

tab = {script.Parent.ScreenGui.one, script.Parent.ScreenGui.two}
function hook()
workspace.remote.OnClientEvent:connect(function(num)
for i,v in ipairs(tab) do
	if v.Value.Value == num then
		script.Value.Value = v.Name
		break
	 end
end
local ui = script.Parent.ScreenGui:FindFirstChild(script.Value.Value)
if ui.selected.Value == true then
	ui.selected.Value = false
	ui.BorderSizePixel = 1
else
ui.BorderSizePixel = 4
ui.selected.Value = true
workspace.k:FireServer()
for i,v in ipairs(tab)do
	if v.Value.Value ~= num and v.selected ~= true then
		v.BorderSizePixel = 1
		v.selected.Value = false
	end
end
end


end)
end
hook()

Basically, I want it to be when you press 1, it'll "select" the UI that corresponds with 1(which it does perfectly right now, that script is far from broken), and then it'll weld the tool that also corresponds with 1 to your hand. Right now, it does this, but it only does it once. After I press 1 again the destroy the tool, it'll destroy it. But if I press 1 again to weld it to my hand again, it wont do anything. Why does it only run once?

1 answer

You don't have a defined loop for the welding bit to run again. To weld the tool to your hand every time, you need to use one of the loops available to use in Lua.

Lua provides the following types of loops to repeat processes more than once: While loops - Repeats a statement or group of statements until the condition is false. For loops - Executes a sequence of statements in defined increments. Repeat loops - Repeats a group of statements until the condition is met.

If you would like more information (and examples) on how to use loops, I recommend reading through this page http://lua-users.org/wiki/ControlStructureTutorial.

If I helped in any way, please give thanks by accepting and upvoting my answer. If you have any further questions, feel free to comment below.

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