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

Script only works once, no error output?

I have a local script that's in a tool, and when you click it, it's supposed to make your character change into one of the materials from an array, but it only works once and won't change anything afterwards. I've been changing things for half an hour and haven't come up with any ideas as to what's wrong with it.

char = game.Players.LocalPlayer.Character:GetChildren()
tool = script.Parent
materials = {"Grass","Wood","Neon"}
m = materials[math.random(#materials)]

tool.Equipped:connect(function(mouse)
	
	mouse.Button1Down:connect(function()
		for i,v in pairs(char) do
			if v:IsA("Part") then
					v.Material = m
			end
		end
	end)
end)

1 answer

✓ Accepted answer
player = game.Players.LocalPlayer
repeat wait() until player.Character --Gotta wait for the character before you can use it.
char = player.Character:GetChildren()

tool = script.Parent
materials = {"Grass","Wood","Neon"}

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
		local m = materials[math.random(#materials)] --I put line 4 down here so it will be a random material every time the mouse is clicked.
        for i,v in pairs(char) do
            if v:IsA("Part") then
				v.Material = m
            end
        end
    end)
end)

1 comment

1

Thanks, it worked. I feel like such a idiot .-. lol, one day i'll be better at this

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