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

Convert this into an on click instead of ontouch?

so ive got this on touch script for a tycoon button, but im wanting to make a billboard with brick buttons on it to buy stuff, problem is i dont know how to convert this ontouch button to an on click button and still work using a clickdetector

ive tried a few things tho im very bad at lua so im sure they were very miss when i did but i guess what i got stuck on was the hitpart throughout the script

mind walking me through how to get this converted?

-------------------------------------------------------------------------------------------------------------------------
model = workspace.Part2
Upgradecost = 0
-------------------------------------------------------------------------------------------------------------------------
upgradeStuff = model:clone()

wait(1)

model:remove()
owner = script.Parent.Parent.Parent.OwnerName 
local ting = 0

function onTouched(hit)
	if ting == 0 then
	ting = 1
	check = hit.Parent:FindFirstChild("Humanoid")

	if check ~= nil then
	if hit.Parent.Name == owner.Value then
		local user = game.Players:GetPlayerFromCharacter(hit.Parent)
		local stats = user:findFirstChild("leaderstats")

		if stats ~= nil then
			local cash = stats:findFirstChild("Gold")
			if cash.Value > (Upgradecost-1) then

				cash.Value = cash.Value - Upgradecost
				upgradeStuff.Parent = script.Parent.Parent.Parent
				script.Parent.Parent:remove()

			end
		end
	end
	end

	ting = 0
	end
end

script.Parent.Touched:connect(onTouched)

3 answers

✓ Accepted answer

THIS IS NOT THE MOST CONVENIENT WAY TO FIX THIS, IT IS THE QUICKEST. So, basically, the first thing you need to do is add a clickdetector to the part. And that's it. Now let me explain what's happening. We will start at where we made the onTouched function. So basically, Onclicked returns the player that clicked it. so I changed the variable name to clicked so it won't return an error in the later script. Then, to conveniently change the script, all I had to do is make a variable named hit, and make it the player's character's head.

-------------------------------------------------------------------------------------------------------------------------
model = workspace.Part2
Upgradecost = 0
-------------------------------------------------------------------------------------------------------------------------
upgradeStuff = model:clone()

wait(1)

model:remove()
owner = script.Parent.Parent.Parent.OwnerName 
local ting = 0

function onTouched(clicked)
    hit = clicked.Character.Head
    if ting == 0 then
    ting = 1
    check = hit.Parent:FindFirstChild("Humanoid")

    if check ~= nil then
    if hit.Parent.Name == owner.Value then
        local user = game.Players:GetPlayerFromCharacter(hit.Parent)
        local stats = user:findFirstChild("leaderstats")

        if stats ~= nil then
            local cash = stats:findFirstChild("Gold")
            if cash.Value > (Upgradecost-1) then

                cash.Value = cash.Value - Upgradecost
                upgradeStuff.Parent = script.Parent.Parent.Parent
                script.Parent.Parent:remove()

            end
        end
    end
    end

    ting = 0
    end
end

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

1 comment

0

thanks for the help, and explaining what you did, works perfect now

Hi...Well this isnt tested but it should work simply convert all your onTouched to onClicked so at line 13

function onClicked(hit)--Thats the first step 

then u need to make a onClicked Connection at the end to make it clickable so on line 40 put

script.Parent.Clicked:connect(onClicked)-- This is your connection and your new line 40

Also put a click detector in. Hope this helped:) Like i said i havent tested it but it should work let me know if it did or didnt.

-------------------------------------------------------------------------------------------------------------------------
model = workspace.Part2
Upgradecost = 0
-------------------------------------------------------------------------------------------------------------------------
upgradeStuff = model:clone()

wait(1)

model:remove()
owner = script.Parent.Parent.Parent.OwnerName 
local ting = 0

function onTouched(hit)
    if ting == 0 then
    ting = 1
    check = hit.Parent:FindFirstChild("Humanoid")

    if check ~= nil then
    if hit.Parent.Name == owner.Value then
        local user = game.Players:GetPlayerFromCharacter(hit.Parent)
        local stats = user:findFirstChild("leaderstats")

        if stats ~= nil then
            local cash = stats:findFirstChild("Gold")
            if cash.Value > (Upgradecost-1) then

                cash.Value = cash.Value - Upgradecost
                upgradeStuff.Parent = script.Parent.Parent.Parent
                script.Parent.Parent:remove()

            end
        end
    end
    end

    ting = 0
    end
end

script.Parent.ClickDetector:MouseClicked(onTouched)

Also, put a click detector inside of the block.

1 comment

0

this didnt work, this is one of the things i tried. just doesnt do anything

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