Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

CanCollide Interaction with Touched:connect?

Asked by 9 years ago
repeat wait() until script.Parent.Product.Value~=nil
local product=script.Parent.Product.Value:clone()
script.Parent.Product.Value:remove()
script.Parent.Parent["Upgrade - 450"].Head.CanCollide=false 
script.Parent.Parent["Upgrade - 450"].Head.Transparency=1
local price=script.Parent.Price.Value


script.Parent.Head.Touched:connect (function(hit)
    local chr=hit.Parent 
    if chr:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(chr) then
        local plr=game.Players:GetPlayerFromCharacter(chr) 
        if plr.Name==script.Parent.Parent.Parent.OwnerName.Value and        plr.leaderstats.Money.Value>=price then
            plr.leaderstats.Money.Value=plr.leaderstats.Money.Value-price
            product.Parent=script.Parent.Parent.Parent
            script.Parent.Parent["Upgrade - 450"].Head.CanCollide=true
            script.Parent.Parent["Upgrade - 450"].Head.Transparency=0
            script.Parent:remove()
        end
    end
end)

Alright, so ideally, at the beginning of the script, it will set up button2 that is dependent on the button1 being pressed. I set the values of Button2 to: Transparancy=1 and CanCollide=false so that in theory, the button2 could not be pressed, or touched in a way that would trigger its own Touch Command to spawn its "Upgrade - 450", and once button1 is pressed and the player had enough money, the "Upgrade - 450" Head would have CanCollide=true and its Transparency=0, so button2 could be pressed.

When I try the script, the transparency values work, however I can still walk over the location where button2 is, and it detects my collission runs its script, even though I don't want it to, until button 1 has been pressed.

Am I just doing this wrong? Can I not set up a Touched:connect function to be dependent on direct collision between player and block?

1 answer

Log in to vote
1
Answered by 9 years ago

The Touchedevent will fire even when a part has its CanCollide set to false.

There are various different other ways you could go about doing what you want to do. My solution would be to put the button somewhere like the Lightingor ReplicatedStorage until it is needed. You could do it like this:

repeat wait() until script.Parent.Product.Value~=nil
local product=script.Parent.Product.Value:clone()
script.Parent.Product.Value:remove()
-- Put the button in the ReplicatedStorage
local Button2 = script.Parent.Parent
Button2.Parent = game.ReplicatedStorage

local price=script.Parent.Price.Value


script.Parent.Head.Touched:connect (function(hit)
    local chr=hit.Parent 
    if chr:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(chr) then
        local plr=game.Players:GetPlayerFromCharacter(chr) 
        if plr.Name==script.Parent.Parent.Parent.OwnerName.Value and        plr.leaderstats.Money.Value>=price then
            plr.leaderstats.Money.Value=plr.leaderstats.Money.Value-price
            product.Parent=script.Parent.Parent.Parent
            Button2.Parent = script.Parent.Parent
            script.Parent:remove()
        end
    end
end)

Ad

Answer this question