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

Is it possible to create change Touched to onClick.MouseClick?

I have this morph and want to make it more realistic by changing the onTouched brick to a ClickDetector.

In this script I have tried to change the bottom part where it says script.Parent.Touched:connect(onTouched) to ClickDetector.MouseClick:connect(onMouseClick) but it doesn't seem to morph. I did insert the ClickDetector.

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Chest") == nil then
		local g = script.Parent.Parent.Chest:clone()
		g.Parent = hit.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" or "Union" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit.Parent.Torso
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end
end

script.Parent.Touched:connect(onTouched)

Please help me, thank you!

1 answer

✓ Accepted answer

The ClickDetector returns the player object, not a part of the character. You need to grab the Character from the player

function onClicked(plr)
	character=plr.Character --Grab the Player's Character
    if character:findFirstChild("Humanoid") and character:findFirstChild("Chest") == nil then
        local g = script.Parent.Parent.Chest:clone()
        g.Parent = character
        local C = g:GetChildren()
        for i=1, #C do
            if C[i].className == "Part" or "Union" then
                local W = Instance.new("Weld")
                W.Part0 = g.Middle
                W.Part1 = C[i]
                local CJ = CFrame.new(g.Middle.Position)
                local C0 = g.Middle.CFrame:inverse()*CJ
                local C1 = C[i].CFrame:inverse()*CJ
                W.C0 = C0
                W.C1 = C1
                W.Parent = g.Middle
            end
                local Y = Instance.new("Weld")
                Y.Part0 = character.Torso
                Y.Part1 = g.Middle
                Y.C0 = CFrame.new(0, 0, 0)
                Y.Parent = Y.Part0
        end

        local h = g:GetChildren()
        for i = 1, # h do
            h[i].Anchored = false
            h[i].CanCollide = false
        end

    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

More on the ClickDetector here

2 comments

3

y no use local variables? :/

0

Bad habit of mine

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