luaroblox

Scripting on Roblox - my button is not working


v.ButtonPart.Touched:Connect(function(Hit)
        if Hit:FindFirstChild("Humanoid") then
            local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
            if Values.OwnerValue.Value == player then
                if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
                    if player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
                        player.leaderstats.Cash.Value -= v.Price.Value
                        items[v.items.Value].Parent = Boughtitems
                    end
                end
            end
        end
    end)

my button is not working.


Solution

  • I think the reason why this isn't working is because of the line: Hit:FindFirstChild("Humanoid"). When you use .Touched:Connect(function()), the first argument of the function is whatever touched it, so if you walked over it, it would return "LeftLeg" or something along those lines. What you're trying to do is find "Humanoid" in whatever part touched it, which definitely won't work. Try replacing if Hit:FindFirstChild("Humanoid") then with if Hit.Parent:FindFirstChild("Humanoid") then.