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

making this into a on enter script?

changing this toa on enter script?


function ChangeMe(hit)
	if hit.Parent == nil then return end
	if (hit.Parent:findFirstChild("Humanoid") == nil) then return end
	local human = hit.Parent:findFirstChild("Humanoid") 
	local char = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if (human ~= nil) and debounce == false then
		debounce = true
		originals = char:getChildren()
		for w = 1, #originals do
			if originals[w].className == "CharacterMesh" then
				originals[w]:remove()
			end
		end
		meshes = script:getChildren()
		for y = 1, #meshes do
			copy = meshes[y]:clone()
			copy.Parent = char
		end
	end
	wait(0)
	debounce = false
end

script.Parent.Touched:connect(ChangeMe)

1 answer

Hello! So this fix is fairly simple! Let me explain it a little bit first.

The way I approach this is by using the PlayerAdded event! It's simple and easy too. Now the problem is how you have you function setup, so I will edit you code to make it compatible with the new code!

Below, is the framework for the new stuff!

function changeMe(player) -- Has the player and is called when a player is added!
end

game.Players.PlayerAdded:connect(changeMe()) -- "PlayerAdded" returns the player thus we can easily edit them!

Now that we have the framework, let's continue with the explaination!

function changeMe(player)
	local character = player.Character or player:WaitForChild("Character") -- Gets the player's character!
	local humanoid = character.Humanoid or character:WaitForChild("Humanoid")
	if (humanoid ~= nil) and debounce == false then
		debounce = true
		originals = character:GetChildren()
		for _, object in pairs(originals) do
			if object:IsA("CharacterMesh") then
				object:Remove()
			end
		end
		meshes = script:GetChildren()
		for _, object in pairs(meshes) do
			copy = object:Clone()
			copy.Parent = character
		end
	end
	wait()
	debounce = false
end

game.Players.PlayerAdded:connect(changeMe())

I definitely changed A Lot of code, but I won't leave you stranded!

If you see, I changed you "for loops" to be MUCH more efficient! Now what they do specifically, is cycle through a table and return two things, the index and the table item! The , object are those two parameters, but since we don't need the index, I wrote mine as a garbage variable or "". Here is also a link for more information on loops, including the one I used!

P.S. Don't copy my code and expect it to fully work! Skim over it to see if there are errors and if there are errors, please let me know!

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